Repository navigation
timers: reschedule timer when _idleStart is written #36859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,6 +220,79 @@ describe("clear", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("_idleStart", () => { | ||
| // https://github.com/oven-sh/bun/issues/26508 | ||
| // Next.js 16 Cache Components writes `t2._idleStart = t1._idleStart` so two | ||
| // `setTimeout(fn)` calls share a deadline and both fire before any | ||
| // `setImmediate` scheduled from `t1`'s callback. | ||
| it("reschedules the timer when written (Next.js pattern)", async () => { | ||
| const script = ` | ||
| async function once() { | ||
| let immediateRan = false; | ||
| let order = []; | ||
| const t1 = setTimeout(() => { | ||
| order.push("t1"); | ||
| setImmediate(() => { immediateRan = true; }); | ||
| }); | ||
| // Force the monotonic clock past a millisecond boundary so t2 would | ||
| // land in a later event-loop turn without the _idleStart assignment. | ||
| { const s = performance.now(); while (performance.now() - s < 2) {} } | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| const t2 = setTimeout(() => { | ||
| order.push("t2"); | ||
| resolve({ immediateRan, order: order.join(",") }); | ||
| }); | ||
| t2._idleStart = t1._idleStart; | ||
| return promise; | ||
| } | ||
| for (let i = 0; i < 50; i++) { | ||
| const { immediateRan, order } = await once(); | ||
| if (immediateRan) { | ||
| console.log("FAIL: immediate ran before t2 on iteration " + i); | ||
| process.exit(1); | ||
| } | ||
| if (order !== "t1,t2") { | ||
| console.log("FAIL: wrong order " + order + " on iteration " + i); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| console.log("ok"); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe("ok"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| it("ignores non-finite values and cleared timers", async () => { | ||
| const t1 = setTimeout(() => {}, 10) as any; | ||
| t1._idleStart = "not a number"; | ||
| expect(t1._idleStart).toBe("not a number"); | ||
| t1._idleStart = NaN; | ||
| expect(Number.isNaN(t1._idleStart)).toBeTrue(); | ||
| t1._idleStart = Infinity; | ||
| expect(t1._idleStart).toBe(Infinity); | ||
| clearTimeout(t1); | ||
| t1._idleStart = 0; | ||
| expect(t1._idleStart).toBe(0); | ||
| }); | ||
|
|
||
| it("does not overflow for extreme finite values", () => { | ||
| const t1 = setTimeout(() => {}, 10) as any; | ||
| t1._idleStart = Number.MAX_VALUE; | ||
| expect(t1._idleStart).toBe(Number.MAX_VALUE); | ||
| t1._idleStart = -Number.MAX_VALUE; | ||
| expect(t1._idleStart).toBe(-Number.MAX_VALUE); | ||
| clearTimeout(t1); | ||
|
Comment on lines
+273
to
+292
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The two auxiliary Extended reasoning...What the bug isTwo auxiliary tests in the new Additionally, the "does not overflow for extreme finite values" test places Code pathFor the "not load-bearing" issue, trace each guard the tests are named after:
Both tests therefore pass with For the leak-on-failure issue: Why existing code doesn't prevent itREVIEW.md is explicit on both counts:
CLAUDE.md also states: "Your test is NOT VALID if it passes with The primary "reschedules the timer when written (Next.js pattern)" test is load-bearing (it fails on Step-by-step proofAuxiliary tests pass on system Bun:
Deleting
Leak on assertion failure:
Note: only the first assertion failing leaks the far-future timer. If the second fails, line 290 has already rescheduled to ms=0 ( ImpactLow. The primary Next.js-pattern test is solid, so the fix itself has real coverage. These two tests are misleadingly named (they suggest guard coverage they don't provide) but not actively harmful — they still document that the cached JS value is preserved for edge inputs, and the extreme-values test implicitly guards against a debug-build overflow abort. The leak scenario requires a nearly-tautological cache round-trip assertion to fail, which is implausible in practice. Neither issue causes a concrete failure if merged as-is, hence nit. FixFor the load-bearing gap, assert on scheduling behavior instead of the cached value. For example: it("ignores non-finite values", async () => {
const start = performance.now();
const { promise, resolve } = Promise.withResolvers();
const t1 = setTimeout(resolve, 10) as any;
try {
t1._idleStart = NaN; // must NOT reschedule to fire-now
t1._idleStart = Infinity; // must NOT reschedule to never
await promise;
expect(performance.now() - start).toBeGreaterThanOrEqual(9);
} finally {
clearTimeout(t1);
}
});Now deleting For the leak, wrap the extreme-values body in try/finally or add it("does not overflow for extreme finite values", () => {
const t1 = setTimeout(() => {}, 10) as any;
try {
t1._idleStart = Number.MAX_VALUE;
expect(t1._idleStart).toBe(Number.MAX_VALUE);
t1._idleStart = -Number.MAX_VALUE;
expect(t1._idleStart).toBe(-Number.MAX_VALUE);
} finally {
clearTimeout(t1);
}
}); |
||
| }); | ||
| }); | ||
|
|
||
| describe.each(["with", "without"])("setImmediate %s timers running", mode => { | ||
| // TODO(@190n) #17901 did not fix this for Windows | ||
| it.todoIf(isWindows && mode == "with")( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.