Repository navigation
Conversation
|
Updated 7:23 AM PT - Oct 17th, 2026
❌ @robobun, your commit 29683ac has 1 failures in
🧪 To try this PR locally: bunx bun-pr 44985That installs a local version of the PR into your bun-44985 --bun |
|
Status Reproduction (Linux x64, bun 1.4.2 and a release build of main)
Tests
Fix
CI
Needs a maintainer's decision
|
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 nits, I also checked that the system_error set by the new fstat arm in do_close is observed by then() (it rejects at src/runtime/webcore/blob/copy_file.rs:160), that the st_mode as _ cast matches the existing S::ISREG call sites in the same file, and that the macOS clonefile chmod-by-path and Windows uv_fs_chmod paths remain unguarded — the description names both as intentionally out of scope. The PR also asks a maintainer to choose between skipping mode and rejecting the write for a non-regular destination; that design call needs a human.
Extended reasoning...
The change adds a single fstat guard in CopyFile::do_close so fchmod only runs on a regular file, plus JSDoc and a POSIX-only test block with FIFO and tty fixtures. It touches no auth, crypto, or input-parsing surface. Two test-convention nits were posted inline, and the author's open question about skip-versus-reject semantics is a behavior decision for a maintainer rather than something this review can settle.
…m Bun.file() CopyFile::do_close called fchmod on the destination with no file-type check. When the destination was an existing FIFO, device or terminal, the copy wrote the data through it and then changed the mode of that shared node. do_close now calls fstat before the fchmod and applies `mode` to a regular file only. A failed fstat rejects the write and skips the fchmod.
…nation matrix The FIFO fixture created the temp dir and then ran the fallible setup (mkfifo, open), so a failed setup left the directory behind. Each test now holds the temp dir with `using` before the setup runs. mkfifo runs through Bun.spawn, and the two destination forms are an it.each table.
c73c01a to
02311df
Compare
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-17. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
Bun.write(dest, Bun.file(src), { mode })changes the mode of a FIFO, device or terminal that exists atdest. On 1.4.2 a FIFO goes from 644 to 600. A script that writes to/dev/stdoutchanges the mode of its terminal.CopyFile::do_close(src/runtime/webcore/blob/copy_file.rs:184) callsfchmodon the destination with no file-type check. fix(io): respect mode option when copying files with Bun.write() #25906 added that call (v1.3.6).Fix
do_closecallsfstatfirst and appliesmodeto a regular file only. The data still reaches the FIFO or device, and the promise resolves.fstatrejects the write and skips thefchmod.test/js/bun/io/bun-write.test.jsfail on 1.4.2 and on main. Alsotest/regression/issue/25903.test.tsand the bun-types test.Background
CopyFileis the thread-pool job behindBun.writefor aBun.file()source. Its open returns an existing FIFO or device in place, sofchmodchanges a node that other processes share.std::fs::copyhas the same check (fs::copy() unix: set file mode early rust-lang/rust#58803). The order open, copy,fchmodstays (Bun.write: applymodefor string, buffer, Blob and stream sources on POSIX #32885 asks about it).copy_file_rangeorftruncatealready proved a regular file on Linux. It saves thefstat, but no test can pin it.Downsides
fstatmore per copy withmode(Linux: 7 to 8 syscalls). It is the only stat of the destination. Withoutmode: 6, as before.EPERMafter the data was written. That call now resolves..textgrows by 256 bytes.Notes
Report. There is no GitHub issue and no user report. The defect was found by a read of the code while #32885 was reviewed. It is in every release since v1.3.6.
Repro (Linux, no root):
Other cases run on Linux 6.1 x64 (bun 1.4.2 and a release build of main against a release build of this branch). The data arrives in every case.
modeBun.file(path)afterexists()(Linux copies it withsendfile)Bun.file(fd)mknod c 1 3node at 666, by path (root)/dev/stdoutEPERM(fchmod) after the data arrivedEPERMafter the data is writtenWorkaround for released versions. Pass
modeonly when the destination is a regular file or does not exist:Measurements. Release builds of main (c6da4a4) and of this diff on that base (the branch was rebased afterwards, the diff is the same). Linux 6.1.155 x64.
Syscalls of the pool thread from the first
opento the lastclose, for a 6-byte copy (gdb syscall catchpoints):mode, regular filemode,BUN_CONFIG_DISABLE_COPY_FILE_RANGE=1mode, existing or new regular filemode, regular file,BUN_CONFIG_DISABLE_COPY_FILE_RANGE=1mode, FIFO or char device by pathfstatin place offchmod)mode, stat'ed FIFOBunFile, or a caller fd on a FIFOInstructions executed in
CopyFile::do_close(gdbstepi): nomode51 to 50.mode+ regular file 64 to 78.Among the
CopyFilesymbols onlydo_closechanges size: 359 to 593 bytes. Release.text65,782,357 to 65,782,613 bytes. The stripped binary has the same file size.size_of::<CopyFile>()stays 496 bytes. The diff adds no field.Test strength. With the non-regular arm of the new check removed, all 4 cases fail. With the check changed to drop the mode for a regular file too, 3 of 4 fail. No test forces
fstatto fail, so nothing pins theErrarm.The flag that is not in this PR. The first version of this branch recorded a proof in
CopyFilewhencopy_file_range(Linux 4.11 and later) or the fallback'sftruncatesucceeded, and skipped thefstatthen. Measured: 7 stays 7 and 11 stays 11 syscalls,.text+512 bytes. A self-review said to remove it: it saves onefstatper copy withmode, no test fails when the proof is deleted, no CI lane runs a kernel older than 4.11, and it added 2 conflict hunks each against #41209, #37856 and #44301. If a maintainer wants the zero-syscall form, it is a follow-up after those PRs.Open PRs on the same lines (conflict hunks counted with
git merge-fileagainst 29 open PR heads, 26 of which touch one of the three files):fchmodblock that follows the new check, so that a descriptor the caller passed does not get the mode. This check is the other half: a destination that Bun opened by path gets the mode only when it is a regular file. After Remove libuv on Windows #42819 lands,self.destination_fd != Fd::INVALIDin the check can becomeclose_input.modefor string, buffer, Blob and stream sources on POSIX #32885: 2 hunks, both JSDoc prose inbun.d.ts. Itsapply_modemakes the same check for the otherBun.writesources. It leavesCopyFilealone and asks whetherCopyFileshould set the mode before the data.copy_file.rsand in the test file.Not changed here
fs.copyFile,fs.cpand the shellcpbuiltin have the same unguardedfchmodinsrc/runtime/node/node_fs.rs. node:fs: do not fchmod a FIFO or device destination in copyFile #43244 is open for them.src/runtime/cli/repl.rs): repl: ignore non-regular history files, and never replace a history file that was not read #38217 is open.CopyFileWindowsappliesmodewithuv_fs_chmodby path. This PR does not change it, and Remove libuv on Windows #42819 deletes that code.fchmodfails withEPERM. That is the order question on Bun.write: applymodefor string, buffer, Blob and stream sources on POSIX #32885.Platforms. Built and run on Linux x64.
cargo check -p bun_runtimepasses foraarch64-apple-darwin,x86_64-unknown-freebsd,aarch64-linux-androidandx86_64-pc-windows-msvc. The macOS and FreeBSD arms were not run here. In CI the FIFO cases pass on the macOS lanes (aarch64 and x64, build 124565). The terminal case is Linux only: macOS opens/dev/stdoutas a dup of fd 1.Question for a maintainer. For a FIFO, device or terminal that
Bun.writeopens by path, this PR writes the data and skipsmode. The alternative is to reject such a destination whenmodeis given. Which one do you want?Self-review. It raised 8 concerns, and all 8 are addressed. The flag is gone, and with it its kernel-version compare, its two-line comment and its untested splice arm. The FIFO cases now run on macOS lanes, the JSDoc is scoped to POSIX, Windows is named above, and the numbers are from the final diff. A later review asked for
it.each, an asyncmkfifospawn and a temp dir that the test body owns. The second commit does that.Suites run (debug build): the 4 new cases, all of
test/js/bun/io/bun-write.test.js(110 pass with--timeout 90000),test/regression/issue/25903.test.ts(7 pass),test/integration/bun-types/bun-types.test.ts(22 pass),cargo clippy -p bun_runtime.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/bun/io/bun-write.test.js