From a3b93814082e1d3a25e71fb5a0ef13ea247e802c Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:17:24 +0900 Subject: [PATCH] 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 --- .../service-worker-page-controller.ts | 7 +++- src/contracts/service-worker.ts | 5 +++ tests/unit/service-worker-runtime.test.ts | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/adapters/service-worker/service-worker-page-controller.ts b/src/adapters/service-worker/service-worker-page-controller.ts index b511a0f7..82237540 100644 --- a/src/adapters/service-worker/service-worker-page-controller.ts +++ b/src/adapters/service-worker/service-worker-page-controller.ts @@ -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( (resolve) => { const container = dependencies.container; diff --git a/src/contracts/service-worker.ts b/src/contracts/service-worker.ts index 0dc5ef66..51004f16 100644 --- a/src/contracts/service-worker.ts +++ b/src/contracts/service-worker.ts @@ -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, diff --git a/tests/unit/service-worker-runtime.test.ts b/tests/unit/service-worker-runtime.test.ts index cab23bfe..98e802f1 100644 --- a/tests/unit/service-worker-runtime.test.ts +++ b/tests/unit/service-worker-runtime.test.ts @@ -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(); + } + }); }); /**