fix(service-worker): give the page activation deadline slack past the drain window (Q-6.6-10b)

The page and the worker both used clientDrainMs (30s). A tab that drained at
29.999s let the worker call skipWaiting() and commit after the page had
already resolved CLIENT_DRAIN_TIMEOUT, so the requesting tab never reloaded.
The page now waits clientDrainMs + activationReplySlackMs (new additive
SERVICE_WORKER_BOUNDS field, 5s).

Verified: RED first with the real page/worker seam (last drain at 29.999s,
skipWaiting 50ms): page resolved CLIENT_DRAIN_TIMEOUT; green after.
Gates: check:architecture, types app/test/service-worker, lint, test:unit
(only pre-existing removal-fixture failure), test:component.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:37:56 +09:00
co-authored by Claude Opus 5.5
parent 3b1830c264
commit a3b9381408
3 changed files with 45 additions and 1 deletions
@@ -381,7 +381,12 @@ export function createServiceWorkerPageController(
}
const nonce = nonces.issue();
const deadline = now() + SERVICE_WORKER_BOUNDS.clientDrainMs;
// Q-6.6-10b. With equal deadlines a tab draining at 29s left this page at
// TIMEOUT while the worker still committed skipWaiting().
const deadline =
now() +
SERVICE_WORKER_BOUNDS.clientDrainMs +
SERVICE_WORKER_BOUNDS.activationReplySlackMs;
const accepted = await new Promise<ServiceWorkerActivationOutcome>(
(resolve) => {
const container = dependencies.container;
+5
View File
@@ -17,6 +17,11 @@ export const SERVICE_WORKER_BOUNDS = Object.freeze({
fetchConcurrency: 4,
installDeadlineMs: 60_000,
clientDrainMs: 30_000,
/**
* Q-6.6-10b. The page waits the worker's whole drain window plus this, so
* a drain that ends just in time is followed by skipWaiting() and the reply.
*/
activationReplySlackMs: 5_000,
updateCheckIntervalMs: 6 * 60 * 60 * 1_000,
retainedPreviousCaches: 1,
reloadGuardBytes: 512,
+34
View File
@@ -1436,6 +1436,40 @@ describe("page ↔ worker activation seam", () => {
vi.useRealTimers();
}
});
it("waits past the worker's drain window for its commit reply (Q-6.6-10b)", async () => {
vi.useFakeTimers();
try {
const seam = activationSeam(["page-build-n", "page-build-n1"]);
for (const tab of seam.tabs) await tab.controller.start();
// The last tab answers 1ms before the worker's drain deadline, and
// skipWaiting() then takes 50ms: the worker commits after 30s.
const slowTab = seam.tabs[0]!.waiting;
const deliver = slowTab.postMessage.bind(slowTab);
slowTab.postMessage = (message) => {
if (message.kind !== "CLIENT_DRAINED") return deliver(message);
setTimeout(
() => deliver(message),
SERVICE_WORKER_BOUNDS.clientDrainMs - 1,
);
};
const promote = seam.workerScope.skipWaiting.getMockImplementation()!;
seam.workerScope.skipWaiting.mockImplementation(async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
await promote();
});
const activation = seam.tabs[1]!.controller.requestActivation();
await vi.advanceTimersByTimeAsync(SERVICE_WORKER_BOUNDS.clientDrainMs + 100);
await expect(activation).resolves.toEqual({
kind: "ACTIVATED_RELOAD_REQUIRED",
});
for (const tab of seam.tabs) await tab.controller.stop();
} finally {
vi.useRealTimers();
}
});
});
/**