From c338e54c33213ca8dd157d30fbc922b65a04c49b Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:27:32 +0800 Subject: [PATCH] fix(process): keep provider waits bounded by the absolute deadline (#11967) --- src/process/ProviderDeadline.test.ts | 30 +++++++++------------------- src/process/ProviderDeadline.ts | 10 +++------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/process/ProviderDeadline.test.ts b/src/process/ProviderDeadline.test.ts index 1e3d7697bc..389140344d 100644 --- a/src/process/ProviderDeadline.test.ts +++ b/src/process/ProviderDeadline.test.ts @@ -121,18 +121,6 @@ it("keeps remaining time and cancellation classification stable across wall-cloc } }); -it("aborts a bounded wait when its real monotonic deadline expires", async () => { - vi.useRealTimers(); - const deadline = new ProviderStartupDeadline(40); - try { - await expect(deadline.wait(500)).resolves.toBe("aborted"); - expect(deadline.interruption).toBe("timeout"); - expect(deadline.remainingMs()).toBe(0); - } finally { - deadline.dispose(); - } -}); - it("does not expire a future monotonic deadline on an early timer callback", async () => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); let monotonic = 0; @@ -222,34 +210,34 @@ it("keeps caller cancellation classified without reporting an exhausted budget", } }); -// Probe from issue #2760 review: both the abort timer and the clamped polling -// timer can fire early at the same monotonic instant. The full-budget wait must -// stay pending until the absolute deadline expires. -it("keeps a full-budget wait pending when both timers fire early", async () => { +it("keeps a full-budget wait pending until the absolute deadline when clocks are sampled at different times", async () => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); - let monotonic = 0; - vi.spyOn(performance, "now").mockImplementation(() => monotonic); + const now = vi.spyOn(performance, "now").mockReturnValue(0); const caller = new AbortController(); const deadline = new ProviderStartupDeadline(40, caller.signal); let outcome: string | undefined; + // Startup polling can be interrupted between sampling its start and + // checking the remaining budget. This must not shorten the deadline. + now.mockReturnValueOnce(0).mockReturnValue(5); const pending = deadline.wait(500).then((result) => { outcome = result; return result; }); try { - monotonic = 39; + // An early timer callback may arrive before the absolute monotonic limit. + now.mockReturnValue(39); await vi.advanceTimersByTimeAsync(40); expect(deadline.signal.aborted).toBe(false); expect(deadline.remainingMs()).toBe(1); expect(outcome).toBeUndefined(); - monotonic = 40; + now.mockReturnValue(40); await vi.advanceTimersByTimeAsync(1); await expect(pending).resolves.toBe("aborted"); + expect(deadline.interruption).toBe("timeout"); expect(deadline.remainingMs()).toBe(0); } finally { caller.abort(); deadline.dispose(); - vi.useRealTimers(); } }); diff --git a/src/process/ProviderDeadline.ts b/src/process/ProviderDeadline.ts index 635a6b8435..cc7edb501c 100644 --- a/src/process/ProviderDeadline.ts +++ b/src/process/ProviderDeadline.ts @@ -100,15 +100,11 @@ export class ProviderStartupDeadline { /** * Wait for at most one polling interval under the shared deadline signal. * - * Both timers may fire before their monotonic targets. Retain the earlier - * of the polling target and deadline, and wait again for any residual time. + * Treat the polling target and the startup deadline as separate absolute times. + * Clamp each timer to the remaining budget and recheck both targets on wake. */ wait(milliseconds: number): Promise { - const startedAt = performance.now(); - // Absolute target: the earlier of the requested polling interval and the - // absolute monotonic deadline. - const targetAt = - startedAt + Math.min(milliseconds, this.#budget.remainingMs()); + const targetAt = performance.now() + milliseconds; const poll = (): Promise => { const budget = this.remainingMs(); const residual = Math.max(0, targetAt - performance.now());