fix(process): keep provider waits bounded by the absolute deadline (#11967)

This commit is contained in:
morluto authored and GitHub committed 2026-11-18 12:27:32 +08:00
1 parent fa22000e43
commit c338e54c33
2 files changed
+12 -28

No files matched your search

+9 -21
View File
@@ -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();
}
});
+3 -7
View File
@@ -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<AbortableWaitResult> {
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<AbortableWaitResult> => {
const budget = this.remainingMs();
const residual = Math.max(0, targetAt - performance.now());