Repository navigation
Conversation
The POSIX reader reports a failed poll registration through on_reader_error and then returns Ok from start(). FileResponseStream went on after that Ok: it took a read ref on a stream that had already finished and read the fd. The bytes were dropped, and the stream stayed allocated, with its fd, when that read did not end in EAGAIN or EOF. start() now returns when the stream has finished.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 1 minute. View limit details
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. Comment |
|
Updated 3:31 AM PT - Oct 24th, 2026
✅ @robobun, your commit 427f4189e9a65193b1eb004fdf424296a5e9b537 passed in 🧪 To try this PR locally: bunx bun-pr 46684That installs a local version of the PR into your bun-46684 --bun |
|
Status: open, ready for a maintainer review. PR: #46684. How I reproduced the bug (Linux x64, main at 7d240c1, debug build with ASAN):
The three new tests in CI for 427f418 (build 131762):
|
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.
Beyond the inline findings (both on the test side), I also checked the new early return in FileResponseStream::start for ref balance: fail_with -> finish has already released the owner ref and dropped any read ref, hold_read_ref is never taken on this path, and the frame-level _guard is the only remaining ref, so the stream (and its fd via Drop) is released when start returns. The fixture's until loops waiting on the second connection closing are bounded by the test timeout, so a regression surfaces as a timeout rather than a hang.
Extended reasoning...
The Rust change is a four-line #[cfg(unix)] early return in src/runtime/server/FileResponseStream.rs after BufferedReader::start, skipping update_ref, hold_read_ref and read when on_reader_error already finished the stream; the rest of the diff is two new Linux/POSIX tests in test/js/bun/http/bun-serve-file.test.ts. No security-sensitive surface is touched. The two inline findings concern test robustness (F_SETPIPE_SZ EPERM on restricted runners, a per-test timeout under ASAN) rather than the fix itself, and the fix sits in refcounted unsafe lifecycle code that a maintainer should still eyeball.
The 200000-byte case set the pipe size with fcntl(F_SETPIPE_SZ). The kernel refuses that with EPERM for a user that holds more pipe pages than fs.pipe-user-pages-soft, so the case could fail on a busy runner. It also carried a per-test timeout for the LeakSanitizer report. The two device tests cover the same leak and need no pipe size.
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.
The two points from my earlier review (the F_SETPIPE_SZ-dependent 200000-byte variant and the per-test ASAN timeout) are addressed by the latest push, which drops both. Beyond the inline finding, I also checked that the new early return at src/runtime/server/FileResponseStream.rs:256 cannot read freed memory: finish() releases only the owner ref while the _guard from RefPtr::init_ref at line 187 still holds one, so the stream is freed (and its fd closed by Drop via auto_close) when the guard drops after the return, not inside on_reader_error. The later re-registration sites (watch()/read() from on_writable) run after hold_read_ref, so their on_reader_error already balances via take_read_ref and need no equivalent check.
Extended reasoning...
The PR adds a 5-line unix-only early return in FileResponseStream::start after the POSIX BufferedReader reports a failed poll registration through on_reader_error and returns Ok, plus two tests in test/js/bun/http/bun-serve-file.test.ts (a Linux-only shared-FIFO fixture and a /dev/zero fd-count test over fetch handler and static route). It touches native refcount and fd lifecycle in the HTTP server but no auth, crypto, or input-parsing surface. The refcount path checks out and the prior review feedback was addressed; the remaining inline finding is pre-existing macOS kqueue behavior outside this fix, and the PR itself flags a reader-contract decision for a maintainer, so a human look is still warranted.
|
Closing as stale: this PR has had no review and no new commit since 2026-10-24. The branch stays, so the work is not lost. To pick it up again, update the branch from main and reopen this PR. |
Problem
Bun.fileresponse whose fd the event loop refuses to watch is closed, but still reads the fd.new Response(Bun.file("/dev/urandom"))leaks one fd per request.new Response(Bun.file(fd))over one pipe: the refused one takes all 1000 bytes. At 200000 bytes LeakSanitizer reportsDirect leak of 216 byte(s)fromFileResponseStream::start.PosixBufferedReader::start()(src/io/PipeReader.rs:553) reports a failed poll registration throughon_reader_error, then returnsOk.FileResponseStream::start()(src/runtime/server/FileResponseStream.rs:250) checks onlyErr.Fix
FileResponseStream::start()returns when the stream has already finished. It no longer reads, or takes the ref that kept the stream allocated.on_reader_errorhas closed the connection and told the owner, so nothing is left to start.test/js/bun/http/bun-serve-file.test.ts(3 new tests fail on main),serve.test.ts,bun-serve-static.Background
FileResponseStreamstreams an fd into a response. For a pipe, socket or device its reader waits on aFilePoll, an epoll or kqueue registration.EEXIST) and a device without poll support (EPERM).start()that returns the error. It changes a contract thatbun lint,bun fmt: write a child's stdin from the event loop #45424 builds on, so it waits for a maintainer.Downsides
sendfileruns 2 more instructions instart()(495 against 493 for a 4 KiB file). Release.textis unchanged.Notes
Where this comes from. No user reported it. A review of #41874 found the read after a failed registration, and #41874 carried this same check (546703b) until it was closed, because #46021 had landed its other parts. #42654 looked at this site with a FIFO and saw no leaked fd. The leak needs a read that ends without
EAGAINand without EOF: a device, or more than 128 KiB in the pipe.Releases (Linux x64,
fetch()againstnew Response(Bun.file("/dev/urandom")), open fds before and after):errorhandler,File must be regular or FIFOECONNRESETECONNRESETThe handler of
docs/guides/http/stream-file.mdxserves the path of the URL, soGET /dev/urandomis this case: 20 requests take the fds from 7 to 27 on main and from 7 to 7 here.Main 7d240c1 against this branch (Linux x64, debug build with ASAN):
finishruns twice. Here: 0, 1 and 1./dev/zero,/dev/urandom,/dev/full: 20 requests take the fds from 7 to 27 on main and from 7 to 7 here./dev/nulldoes not leak on main, because its first read is an EOF.Direct leak of 216 byte(s) in 1 object(s)on main, clean here.Cost (release builds of 5c6d4e2 with and without the change):
llvm-size:.textis 82,231,705 bytes in both.FileResponseStream::startis 1154 bytes, against 1156.FileResponseStream::startand in the reader's own functions, with other calls stepped over in gdb: 493 -> 495 for a 4 KiB regular file, 725 -> 727 for a FIFO with data and EOF, 734 -> 736 for a FIFO with data and a writer.The reader-wide change. The contract behind this bug is in
PosixBufferedReader::start(). It reports a failed registration by callback and returnsOk, so each parent has to check its own state afterwards. Four parents do:SubprocessPipeReader.rs:211,shell/subproc.rs:1836,Terminal.rs:446, and thestart_erradapter ofshell/IOReader.rs. This PR adds the fifth check.I also built the change where
start()returns the error and all 19 POSIX call sites handle it. It is the branchrobobun/1dedb473/pipe-reader-start-returns-err: one commit on top of this PR, 14 source files, +80/-86. It removes all five checks. Numbers for that branch:.text: +2,304 bytes.Bun.spawnpipe start.epoll_ctlfailure injected, 14 of 16 parent fixtures print the same as main in 20 runs each. The other two arebun run --filter, where a debug assertion on main no longer fires, and a fixture with two racing scripts.It is not in this PR, for three reasons:
bun lint,bun fmt: write a child's stdin from the event loop #45424 adds a caller that relies on the callback (captured_child.rs: "If this fails, it has reported throughon_reader_error").start()would keep its signature, sobun lint,bun fmt: write a child's stdin from the event loop #45424 would merge without a conflict and then wait forever on a failed registration.Erris not decided. The POSIX writers release it (io: leave the fd with the caller when a POSIX pipe writer fails to start #38354). The reader on main keeps it.That change needs a maintainer decision on those three points first.
Other open PRs on these lines.
start_reader(). It needs the same check there.bun-serve-file.test.ts. Keep both blocks.Tests.
EV_ADD(src/io/posix_event_loop.rs:687), which replaces the first registration and does not fail. The first response then gets no more events, and itsEV_DELETEat teardown (:998) removes the watch that the second response uses. A fix for that belongs in the kqueue registration ofFilePoll, not in this PR. I read this from the code and did not run macOS.fcntl(F_SETPIPE_SZ)withEPERMfor a user that holds more pipe pages thanfs.pipe-user-pages-soft, and the first version of that test failed for such a user.scripts/runner.node.ts, the test process also reportsDirect leak of 3456 byte(s) in 16 object(s)fromFileResponseStream::start. They also run on macOS.should use correct error when using a root range port(#7187)inserve.test.tsfails in my container on main too: the container runs as root.no test proof · iteration 2 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/http/bun-serve-file.test.ts