Repository navigation
Conversation
A pull on a polled fd read the fd again inside the delivery of the chunk before it, also right after a read that had found the fd empty. Bytes that a child wrote while a 'data' listener ran arrived in the same turn of the event loop. A listener detached until the next setImmediate missed them, execFile() lost its callback when the decoder held such a chunk back, and 'readable' stalled. request_read() replaces read_into(). After a short read it arms the poll and the poll callback delivers the next bytes in a later turn, as libuv does. After a read that filled its buffer the pull still reads straight into the pull buffer. resume_reading() does the same for a resumed stream. The read loop arms the poll before it delivers a short read to a consumer that waits for it, so a child's 'exit' stays behind the bytes the child wrote first.
Without a pidfd a thread waits for child processes and reports an exit as a task. Tasks run before the poll, so the bytes that a child wrote before it exited would arrive after its exit. A pull keeps its read in that mode. A read of a blocking pipe that fills its buffer is not a short read, whatever the size of that buffer. A nested read loop reads into a smaller buffer than the scratch. The exit order fixture treats a child that a thread already reaped as exited. The tests of this behaviour are skipped when Bun has no pidfd.
A Node stream over a native source (child.stdout, Readable.fromWeb(proc.stdout)) sets PosixFlags::POLL_ONLY on its reader. request_read then waits for the poll callback after every read, not only after a short one, so the stream pushes no chunk inside _read() after its first read. Readable emits a chunk pushed inside _read() before the nextTicks and promise jobs of the chunk before, and a loop of `await once(stream, "data")` lost it. The read loop stops for such a consumer when it paused the reader inside a delivery, and after 2 MiB in one wakeup. Subprocess::on_process_exit reads the pipe of a stream whose pull waits before it reports the exit. What a 'data' listener of child stdio throws is an uncaughtException, and the stream reads on when a handler took it: reportUncaughtException returns whether one did.
|
Updated 12:30 AM PT - Nov 14th, 2026
❌ @robobun, your commit d3f542e has 1 failures in
🧪 To try this PR locally: bunx bun-pr 48918That installs a local version of the PR into your bun-48918 --bun |
|
Status How I reproduced it (Linux x64, release builds of f336318):
This PR targets the branch of #48782 and moves into it. It is a draft until the macOS lanes have run. |
|
#48955 targets this branch. It makes Measured on Linux x64, release builds. A loop of
The first row is a loss that main does not have. #48955 changes two things of this PR:
One finding that #48955 does not change: a fill of a socket reports the EOF with the last bytes. With |
f7101f8 to
d0efd5f
Compare
|
#48782 now carries this work in its own form, on a fresh base:
Not taken: the bound of 2 MiB per wakeup of the read loop and the stop of the read loop at a hang-up. The read loop in #48782 is main's code. #48955 targets this branch. It needs a rebase onto #48782: Closing in favor of #48782. |
This PR moves into #48782 (merge it into that branch). Related to #48514.
Problem
await once(child.stdout, "data")loses bytes and exits with code 0. Complete runs of 10: main 0, FileReader: a Node stream takes each chunk from the poll callback #48782 5, Node 10.PosixBufferedReader::request_read(src/io/PipeReader.rs) reads inside_read()(Streams: one PipeReader loop with owned chunks, hold-not-adopt buffer pins, right-sized native pulls #38886).Readableemits that chunk before the promise jobs of the chunk before.'exit'(main: all).Fix
constructNativeReadablesetsPOLL_ONLY:request_readleaves every read to the poll callback, as Node's_read()does. One wakeup delivers at most 2 MiB, as in libuv.Subprocess::on_process_exitreads the pipe of a waiting pull before it reports the exit. A throwing'data'listener of child stdio is anuncaughtException.child-process-stdio.test.js(16 new, 9 fail on FileReader: a Node stream takes each chunk from the poll callback #48782), 162 of 163 scripted checks. Self-reviewed: 22 concerns raised, 13 addressed (rest: Notes).Background
NativeReadableis the Node stream over a native source:child.stdout,Readable.fromWeb(proc.stdout).net.Socketstdio (child_process: make piped stdio streams instances of net.Socket #36316, Notes).Downsides
process.stdin, Linux without pidfd. No macOS or Windows run before CI.Notes
Where this comes from. An automated report, not a GitHub issue: a loop of
await once(child.stdout, "data")over 2,000,000 bytes from/bin/catends short with exit code 0 on Bun 1.4.0 to main. #48514 is the same mechanism seen through'readable', and #48782 handles that form. #48782's status comment names this loop as its open case. All numbers are from Linux x64. The release builds are f336318 ("main"), that commit with #48782'sPipeReader.rsandFileReader.rs("#48782"), and that commit with #48782's source and this PR ("this PR"). The #48782 build also has theboolreturn ofBun__reportUnhandledError, which nothing reads there.Why one PR with #48782. #48782 alone is worse than main in these cases, and this diff repairs each of them:
'exit'(stdout:late,web:late). main: all.Bun.spawn()that stopped seesexitedsettle with 131,076 of 163,844 bytes (reader:late). main: all.'data'listener that throws at the second chunk ends the process with status 1 although an'uncaughtException'listener is installed (stdout:throw:second). main: status 0, all bytes.The mechanism. A chunk that the native pull returns inside
_read()is pushed inside_read().Readableholds such a chunk, andflow()emits it in the same tick as the chunk before. The nextTicks and promise jobs of the listeners of the chunk before have not run then, so a listener that they attach misses the chunk. #48782 lets the pull wait after a read that came up short. After a read that did not find the pipe empty (128 KiB or more in one go, or 64 KiB of a blocking pipe), its pull still reads. #38886 added that read (read_into), and this PR turns it off for Node streams on the sources that #48782 calls poll-driven. Bun 1.3.13, Node, and Bun on Windows deliver every chunk from the loop callback.The reported loop (complete runs):
await once(child.stdout, "data"), 2,000,000 bytes fromcat, 10 runsReadable.fromWeb(Bun.spawn().stdout)process.nextTickbetween theonce()calls, 16 MiB (bench/child_process/stdio.mjs once), 5 runsWhat changed, site by site.
PosixBufferedReader::request_read: one more flag in the test that makes a request wait (SHORT_READ | POLL_ONLY).read_loop, for aPOLL_ONLYconsumer only:_read()got those bytes at once.MAX_BYTES_PER_WAKEUP) when the consumer's request inside a delivery armed the poll. libuv stops after 32 reads of 64 KiB (stream.c).constructNativeReadablecallssetPollOnly()on every source. Only the file source has it, and it has an effect only where FileReader: a Node stream takes each chunk from the poll callback #48782'sis_poll_driven()holds. Its third parameter is spelled as in child_process: keep 'close' of stdout and stderr when their 'error' emit throws #44849 ("libuv-handle" | "webstream"), and the hunks of the two callers are the same as there.Subprocess::on_process_exitcallsFileReader::read_before_exitfor the stdout and stderr stream that JS took. It reads when a pull waits on a poll-driven source. That covers a web reader too.pushAndCheckcatches what a'data'listener of child stdio throws and reports it withreportUncaughtException, which now returns whether a handler took it. The stream reads on only then. The five hunks of that return value are byte for byte those of child_process: keep 'close' of stdout and stderr when their 'error' emit throws #44849.Why the exit needs a read. A stream that stopped its reader arms its poll again at the next
_read(). When the child has exited by then, its pidfd is ahead of the pipe in epoll's list, and the exit came before the last bytes. libuv reports a child's exit after the I/O of the same poll (linux.c). An earlier build of this work held child exits back in the poll dispatch. A self-review found a use-after-free in it (a nested loop tick reports the same level-triggered pidfd twice). It is not in this PR.The bound. With every read of a Node stream in the poll callback, a child that stays ahead of a slow listener kept the loop in one wakeup. Probe:
yesas the child, a'data'listener that is busy 6 ms for each chunk, a 10 ms interval, 3 s, 6 runs each.'data'events in one loop turnThe read before the exit has the same bound. Without it the exit of
sh -c 'yes & exit 0'was not reported whileyesstayed ahead.Tests. All in
test/js/node/child_process/child-process-stdio.test.js, beside #48782's, in the block "a stdio stream read one event at a time" (16 tests).laterforms (a 5 s stall), three counts of pushes inside_read(), the bound per turn, the exit forms, and the listener throw. main fails 17 of the 36 in the file.process-stdin.test.ts24,node-stream.test.js123,spawn-pipe-start-error.test.ts24,shell-pipe-read-fault.test.ts11,spawn-stdio-syscall-error.test.ts17,child_process-node.test.js30, the AOT rows test 114 (one new case holds theboolofreportUncaughtException).child_process.test.ts: 83 pass and 3 fail, and the same 3 fail on main here (10,240 open fds against the debug build's JS files, a 5 s GC wait, and a uid test that depends onPATH).child-process-stdio-once-data-fixture.jshas five kinds of stream:stdoutandstderrof a child,web(Readable.fromWeb(Bun.spawn().stdout)),stdin(Readable.fromWeb(Bun.stdin.stream())behind a shell pipe, the one blocking pipe in the mode) andreader(a reader of the web stream). Under Node it prints the same object forstdoutandstderr, but for the length of the first chunk.child_process.test.tsno longer reaches the read nested in the read loop on Linux with pidfd and on macOS: that pull waits now. The waiter-thread test here reaches it (stdout:exit:full) and asserts the chunk lengths 65,536 and 32,768 that only a nested read gives.child_process.test.tsis not touched.BunPlugin.cpp(Include wtf/Scope.h in BunPlugin.cpp #48812, open). My local debug builds carry that one include. This PR does not.cargo checkforx86_64-pc-windows-msvcandaarch64-apple-darwin. No test ran on macOS or Windows before CI. The exit forms and the bound tests need/procand run on Linux only. The state "more than 256 KiB queued at a hang-up for a stopped stream" needs macOS's 512 KiB sockets and has no test.The 163 scripted checks. They are scripts from the findings of the reviews of this work, each judged against Node, main or Bun 1.3.13. They are not in the tree. main 150 pass. #48782 149: it fails 5 that main passes and leaves 7 loop checks failing. This PR 162 in one full run of the final build. The one failure is a
Duplex.fromover a Blob, which fails on every Bun build.Costs (this PR against #48782, same commit and toolchain, main in brackets).
setPollOnly).child.stdout, 64 MiB,'data'listener: 2.00 (1.63 to 1.77) [0.49 to 1.28].child.stdout(recvfrom+epoll_ctl+epoll_pwait2), 3 runs: 550 to 778 (506 to 678) [748 to 1,403]. A child that answers 16 bytes per round: 2.00recvfromand 2.00epoll_ctlper round (the same) [3.00 and 3.00].'data'1,213 to 1,214 (1,255 to 1,266),'readable'1,236 to 1,238 (1,242 to 1,245),for await1,311 to 1,331 (1,315 to 1,331),pipe()1,287 to 1,290 (1,329 to 1,333).process.stdinon a shell pipe, which this PR does not touch, as the control: 1,350 to 1,360 (1,345 to 1,376)./bin/true, stdout piped): 560,277 instructions (557,883) [558,696]. The three are inside the noise of this measurement.Readable.fromWeb(Bun.stdin.stream())on a shell pipe, 64 MiB in 1,024 chunks:read1,060,epoll_ctl1,028,epoll_pwait21,024,poll1,026, together 4,138 (4,284 to 4,326) [5,970 to 5,995]. One chunk for each loop turn.child.stdoutwith a'data'listener, 7 interleaved runs, median ms (range): 670 (494 to 1,037), FileReader: a Node stream takes each chunk from the poll callback #48782 982 (812 to 1,439), main 1,041 (701 to 1,653), Bun 1.3.13 711 (511 to 1,036), Node 490 (334 to 564). The ranges overlap.'readable',for await,pipe()and thewebstream at 1 GiB: no median is outside the range of main.Downsides, with the numbers.
destroy(), then 6 collections:'readable'listener reads nothing and is removed after'exit'): main keeps 17 to 19 sockets, FileReader: a Node stream takes each chunk from the poll callback #48782 17 to 19, this PR 17 to 19 (3 runs each). Bun 1.3.13 keeps 0, and Node 26.3 keeps 20 (with 69,632 bytes, which is what fits its pipe after the stop)._read()inside the call.process.stdinstill loses chunks in the loop with anextTick: complete in 0 of 5 runs over 8 MiB from a shell pipe on main, on FileReader: a Node stream takes each chunk from the poll callback #48782 and here (Node and Bun 1.3.13: 5 of 5). It has its own adapter inProcessObjectInternals.tsand shares its stream withBun.stdin.stream().BUN_FEATURE_FLAG_FORCE_WAITER_THREAD=1): FileReader: a Node stream takes each chunk from the poll callback #48782'sis_poll_driven()is false there, so the flag has no effect. spawn: report a waiter-thread child exit after the loop's next poll #43888 (closed) tried to move that exit behind the next poll.Not changed.
WindowsBufferedReader::request_readnever reads on the caller's stack. No test of this PR runs there.Blob,Response, a regular file) still answer a pull at once. node:stream: pull a native-backed Readable from a later tick, never inside _read() #48551 is about those._read().'exit'against the bytes ofstdio[3]and above (net.Socket).Self-review. 22 concerns, 13 addressed: the bound, the stop for a paused reader (gated to the flag, and at a hang-up), the exit read for web readers, the helper that skipped the second arm (removed, one skip stays for a blocking pipe and is measured above), the guard in
readAfterListenerThrow(removed: four probe forms give the same events without it), thestdinkind, thewebexit forms, the rethrow forReadable.fromWeb(), the nested read under the waiter thread, the parameter spelling of #44849, and the texts.Not done:
/proc), and Streams: one PipeReader loop with owned chunks, hold-not-adopt buffer pins, right-sized native pulls #38886's six throughput rows with RSS. The 1 GiB rows above are what this machine resolves.Questions for a maintainer.
net.Socket. A preload that hands them tonet.connect({ fd })on main passes 160 of the 163 checks. It failsread()inside'exit'and a destroy stress check, and it skipsexec,execFileandfork. Under that route the listener-throw part of this PR loses its POSIX caller. The flag, the stop and the exit read stay forReadable.fromWeb()over a pipe. Is the flag wanted until then?Subprocess::on_process_exitreaches the stream through the cached JS value. Is that the right home for it?Related open PRs. #48782 (the base of this branch). #44849 (the same
boolreturn and the same parameter, identical hunks). #37171 (the listener throw for child stdio, which reads on also with no handler, and so turns exit status 1 into 0). #43790 (orders the events of child stdio innative-readable.ts). #48553 (the order of the pull buffer, not needed here). #48551 and #48652 (native-readable.tsfor sources that answer at once). #48879 (pipe fds innet, the route of #36316). #48812 (the include that the debug build needs).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/child_process/child-process-stdio.test.js, test/aot/builtins/rows-of-the-host-are-held-by-calls-written-by-hand.test.ts