Repository navigation
Conversation
…allback order test The test "keeps a non-throwing callback in the same place in the event loop" called fs.stat() and setImmediate() from the main script, and expected the fs callback to run first. The stat runs on the thread pool. The immediate runs first when the stat is not complete at the first immediate phase. Node prints either order too. The callback now queues the immediate. The expected output is the same, and it no longer depends on the thread pool.
|
Updated 5:16 PM PT - Oct 21st, 2026
✅ @robobun, your commit 075029ac86e93e840c2ba328708693fd35c3e8fa passed in 🧪 To try this PR locally: bunx bun-pr 46144That installs a local version of the PR into your bun-46144 --bun |
|
Status: the change is ready for review in #46144. How to reproduce the failure
On a release build of |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe filesystem event-loop ordering test now waits for pending filesystem tasks before scheduling ChangesEvent-loop ordering test
Suggested reviewers: Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
LGTM — a test-only de-flake that keeps the deterministic half of the original ordering assertion (callback, then the nextTick it queued, then the immediate it queued) and drops only the part that raced the thread pool in Node as well.
Extended reasoning...
The diff touches a single test in test/js/node/fs/fs.test.ts (lines 7218-7233): it moves one setImmediate call from the main script into the fs.stat callback body and adds a two-line comment; the expected output string and the other 17 cases in the describe block are unchanged, and no production code is involved. It touches no security-sensitive surface. The new ordering (tick-from-fs-cb before setImmediate, both queued from the callback) is deterministic because the nextTick queue drains after the task callback returns and immediates run in a later loop phase, so the queueing order in the script (immediate first) is not what the assertion reflects. The file is not covered by .github/CODEOWNERS, the change is small and self-contained, and the one inline finding is a naming/intent nit rather than a correctness problem.
… from the main script The previous commit moved the setImmediate into the fs.stat callback. That made the order fixed, but the test no longer checked where the callback runs in the loop: a callback that moves to an immediate phase printed the same line. The main script queues the immediate again, as the test did at first. Before that, the script spins until the completion of the stat is in the concurrent queue (getEventLoopStats().concurrentTasksEmpty). The loop then runs the completion before the immediate on every run.
…2b6afe/fs-callback-immediate-order
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
LGTM — small test-only de-flake, and the restructure since my last pass (immediate queued from the main script again, after the spin) means the test once more asserts the callback's event-loop position rather than ordering within the callback.
Beyond the inline nit, I checked that the spin can actually exit: fs.stat is AsyncFSTask on every platform (only open/read/write/close/statfs use the libuv UVFSRequest path on Windows), so its completion always posts to the regular loop's concurrent_tasks, which is the queue getEventLoopStats() reads for a top-level script; bunEnv sets BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING, so the child can load the hook.
Extended reasoning...
The diff touches one test in test/js/node/fs/fs.test.ts, adding a bun:internal-for-testing import and a spin on concurrentTasksEmpty between fs.stat and setImmediate in a spawned child; no production code and no security-sensitive surface. The assertion and test name are unchanged, so the original property is still asserted. I traced the hook (src/jsc/event_loop.rs get_active_tasks) and the stat task type (src/runtime/node/node_fs.rs, AsyncFSTask on all platforms, delivered via VmHandle::deliver into the regular loop's concurrent_tasks) to confirm the spin terminates. The only finding is a nit about the lack of a deadline on the spin, and no CODEOWNERS entry covers the file, so approval is appropriate.
| }); | ||
| // The stat completes on the thread pool. Spin until its completion is queued for this | ||
| // thread. Otherwise the immediate runs first when the stat is slow. | ||
| while (getEventLoopStats().concurrentTasksEmpty) {} |
There was a problem hiding this comment.
🟡 nit (optional): Maintainers get a bare test timeout instead of the ordering assertion if the stat completion ever stops landing on the concurrent queue. The spin at test/js/node/fs/fs.test.ts:7408 has no deadline, which CLAUDE.md's "poll with a deadline" rule asks for. Fix: bound the spin (for example a Bun.nanoseconds() deadline of a few seconds) and fall through to setImmediate, so the existing toBe at test/js/node/fs/fs.test.ts:7412 reports the actual order and the child never pins a core until the runner kills it.
Why this was flagged
The child spins on getEventLoopStats().concurrentTasksEmpty at test/js/node/fs/fs.test.ts:7408, which reads event_loop.concurrent_tasks.is_empty() in src/jsc/event_loop.rs:1409. That only turns false when the pool thread posts the AsyncFSTask completion through Ticket.post -> deliver -> concurrent_tasks.push (src/jsc/VmHandle.rs:180). Today fs.stat is AsyncFSTask on every platform (src/runtime/node/node_fs.rs:604), so the spin ends; but if stat were ever routed like Open/Read through UVFSRequest on Windows (src/runtime/node/node_fs.rs:583, 690) or another queue, the push never happens and the loop never exits. The child then burns 100% of one core until the test's default timeout and the runner's dangling-process kill, and the test reports a timeout with no stdout rather than the ordering mismatch the base branch would print. CLAUDE.md:107 says to poll with a deadline; the PR text says the runner kill is the deadline, but that trades the diagnosable assertion for a timeout.
Verification: nit — triggers only if a future runtime change delivers the fs.stat completion other than through the concurrent task queue, in which case the child spins until the test timeout instead of the toBe at line 7412 reporting the order. test/js/node/fs/fs.test.ts:7408 is while (getEventLoopStats().concurrentTasksEmpty) {} with no deadline. So nothing breaks today. CLAUDE.md:107 says "poll with a deadline or await the event itself".
Problem
fs.test.ts"keeps a non-throwing callback in the same place in the event loop" fails in 25 of 30 runs on a release build.Expected: "fs-cb:true:true,tick-from-fs-cb,setImmediate",Received: "setImmediate,fs-cb:true:true,tick-from-fs-cb".fs.stat(), thensetImmediate()(test/js/node/fs/fs.test.ts:7226at3880151f0a). The stat runs on the thread pool. The immediate runs first when the completion is not queued at the first immediate phase.Fix
fs.stat(), the child script spins untilgetEventLoopStats().concurrentTasksEmptyis false. Then it queues the immediate. The assertion and the test name do not change.test/js/node/fs/fs.test.ts. On one release build the old test passes 5 of 30 runs and the new test passes 200 of 200. It passes withbun bd test.Background
node:fscall runs on a thread pool. Its completion returns to the JS thread through the concurrent task queue, whichEventLoop::tick()runs.tick(), thenauto_tick_active(), which runs the immediates first (src/runtime/jsc_hooks.rs). Node has the same order: libuv's poll phase calls fs callbacks, and its check phase calls immediates.getEventLoopStats()is a hook ofbun:internal-for-testing(src/jsc/event_loop.rs:1384).concurrentTasksEmptyreadsis_empty()of that queue.process-stdin.test.tspolls the same hook.Notes
Measurements. Linux x64, 12 cores. The runtime of every bun build in these tables is main at
3880151f0a, the base of the first two commits. "fs first" is the line that the test expects.Through
bun test test/js/node/fs/fs.test.ts -t "keeps a non-throwing callback in the same place":The child script, run directly:
Node. Node has no such hook, so the Node variant of the new script blocks the main thread for 50 ms between
fs.stat()andsetImmediate(). Node then prints the fs callback first in 200 of 200 runs. So the order that the test asserts is also the order of Node when the stat is complete before the loop starts.The wait. The concurrent queue was empty before
fs.stat()in 200 of 200 release runs and 60 of 60 debug runs. Nothing else in the child script posts to that queue, so the completion of the stat ends the spin. From thefs.stat()call to the end of the spin, the release build took 0.6 ms to 13 ms (median 3.3 ms) and the debug build took 6 ms to 22 ms. That is the window that the old test raced.The test still checks where the callback runs. I ran two variants of the script in which the completion calls the real callback from
setImmediate(), as a runtime with that regression does. The new script printssetImmediate,fs-cb:true:true,tick-from-fs-cbin 100 of 100 runs, so the test fails. The first commit of this PR (the callback queues the immediate) prints the expected line in 100 of 100 runs, so it does not see the regression.Why the spin has no deadline. The test is not concurrent, so the runner stops the child when the test times out (
kill_dangling_processes_on_timeoutinsrc/runtime/test_runner/Execution.rs). I checked this with a child that spins forever: the runner printskilled 1 dangling processand no child stays.Other reports. #45377 reports this failure on a Linux release build. #42014, #42276 and #43774 report it on Windows debug builds.
Related. #36548 and #41355 (open) change when the loop runs thread-pool completions and immediates. This test pins one order of the first turn: a completion that is queued before the loop starts runs before an immediate from the main script, as in Node. I did not run the test on those branches.
The same pattern in other tests. I searched
test/js/node(without the vendored Node tests),test/js/bun,test/js/web,test/cliandtest/regressionfor an exact order between a thread-pool callback and an immediate or a timer from the same turn. No other test expects the thread-pool callback first.test/js/web/streams/compression.test.ts:441expects the opposite on purpose: an immediate runs before a 2 MiB gzip completes.After the merge of main. The branch now includes main at
963452075e. On a debug build of the merged tree the test passes 20 of 20 runs throughbun test, and the new script prints the expected line in 100 of 100 runs. The concurrent queue was empty beforefs.stat()in 60 of 60 runs. In one of those runs the completion took 551 ms to arrive. The new test waits for it.Other runs. The whole file on the release build: 617 pass, 8 skip, 0 fail in 8 of 9 runs. The first run had 6 failures in other tests (5 stream tests that failed in 1 ms each, and a timeout in
fs.statfsSync preserves values above i32::MAX (#31510)). I did not capture their output, and they did not occur again in the 8 runs after it. The whole file withbun bd test: 613 pass, 4 fail. The 4 arereaddir ... should work x 100tests: three time out at 10 s, and one fails after them. Main does the same in my container. Thedescribeblock of the changed test passes: 18 of 18. I did not run Windows or macOS.no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/fs/fs.test.ts