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(); + } + }); }); /**