Skip to content

test(node:fs): wait for the stat completion before queuing the immediate - #46144

Closed
robobun wants to merge 3 commits into
mainfrom
robobun/c32b6afe/fs-callback-immediate-order
Closed

robobun wants to merge 3 commits into
mainfrom
robobun/c32b6afe/fs-callback-immediate-order

Conversation

@robobun

@robobun robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

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".
  • The child script calls fs.stat(), then setImmediate() (test/js/node/fs/fs.test.ts:7226 at 3880151f0a). The stat runs on the thread pool. The immediate runs first when the completion is not queued at the first immediate phase.

Fix

  • After fs.stat(), the child script spins until getEventLoopStats().concurrentTasksEmpty is false. Then it queues the immediate. The assertion and the test name do not change.
  • Correct because the completion is then queued before the loop starts. Each loop turn runs the queued completions before the immediates.
  • A callback that moves to an immediate phase still fails the test (Notes).
  • Verified: 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 with bun bd test.

Background

  • An async node:fs call runs on a thread pool. Its completion returns to the JS thread through the concurrent task queue, which EventLoop::tick() runs.
  • A loop turn is tick(), then auto_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 of bun:internal-for-testing (src/jsc/event_loop.rs:1384). concurrentTasksEmpty reads is_empty() of that queue. process-stdin.test.ts polls the same hook.
  • Considered the first commit, where the callback queues the immediate. It fixed the order but no longer checked where the callback runs.
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":

binary old test new test
release build 5 of 30 pass 200 of 200 pass
debug + ASAN build not measured 30 of 30 pass

The child script, run directly:

binary old script, fs first old script, immediate first new script
release build 183 117 300 of 300 fs first
release build, 24 processes at a time 437 523 960 of 960
debug + ASAN build 98 2 100 of 100
Node v26.3.0 21 179 see below

Node. Node has no such hook, so the Node variant of the new script blocks the main thread for 50 ms between fs.stat() and setImmediate(). 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 the fs.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 prints setImmediate,fs-cb:true:true,tick-from-fs-cb in 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_timeout in src/runtime/test_runner/Execution.rs). I checked this with a child that spins forever: the runner prints killed 1 dangling process and 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/cli and test/regression for 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:441 expects 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 through bun test, and the new script prints the expected line in 100 of 100 runs. The concurrent queue was empty before fs.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 with bun bd test: 613 pass, 4 fail. The 4 are readdir ... should work x 100 tests: three time out at 10 s, and one fails after them. Main does the same in my container. The describe block 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

…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.
@robobun

robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 5:16 PM PT - Oct 21st, 2026

✅ @robobun, your commit 075029ac86e93e840c2ba328708693fd35c3e8fa passed in Build #130235! 🎉


🧪   To try this PR locally:

bunx bun-pr 46144

That installs a local version of the PR into your bun-46144 executable, so you can run:

bun-46144 --bun

@robobun

robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: the change is ready for review in #46144.

How to reproduce the failure

  1. Build a release binary of main: bun scripts/build.ts --profile=release.
  2. Run the test 30 times with that binary: bun test test/js/node/fs/fs.test.ts -t "keeps a non-throwing callback in the same place".

On a release build of 3880151f0a (Linux x64, 12 cores), 25 of 30 runs fail with Received: "setImmediate,fs-cb:true:true,tick-from-fs-cb". With this change, 200 of 200 runs pass on the same binary.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 6a27c13b-a9aa-43c2-a1e5-351a7205f3c3
📥 Commits

Reviewing files that changed from the base of the PR and between 788654e and 075029a.

📒 Files selected for processing: 1
  • test/js/node/fs/fs.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The filesystem event-loop ordering test now waits for pending filesystem tasks before scheduling setImmediate. This preserves the expected order of the filesystem callback, its process.nextTick, and setImmediate.

Changes

Event-loop ordering test

Layer / File(s) Summary
Filesystem completion and callback ordering
test/js/node/fs/fs.test.ts
The test imports getEventLoopStats and waits until concurrent filesystem tasks are no longer pending before scheduling setImmediate.

Suggested reviewers: cirospaciari

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 07502

No verified merge-blocking behavior remains in the supplied evidence.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main test change: waiting for stat completion before scheduling setImmediate.
Description check Passed The description explains the problem, the fix, the rationale, and verification results. It provides the information required by the template, although it uses different headings from the template.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/js/node/fs/fs.test.ts Resolved
… 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.
@robobun robobun changed the title test(node:fs): queue the setImmediate from inside the fs.stat callback test(node:fs): wait for the stat completion before queuing the immediate Oct 21, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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".

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Folded into #49196, which combines the open test-only PRs into one. The commit 7fad53f30 in that PR is this change, unchanged.

Closing this one in favor of #49196.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant