Repository navigation
Conversation
…e ones History::load opened ~/.bun_repl_history with a plain blocking open and read the whole file, so a FIFO at that path hung `bun repl` before it printed anything (and History::save hung it again at exit), and a huge file was read into memory in full before being trimmed to the last 1000 entries. Both now go through History::open_file, which opens with O_NONBLOCK and skips anything that is not a regular file. load reads at most the last MAX_HISTORY_FILE_BYTES of the file and collects entries from the end, which also replaces the quadratic remove(0) trimming loop.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughREPL history loading and saving now use a shared file-opening helper that rejects non-regular files. New tests cover persistence, history limits, and behavior with symlinks, FIFOs, device nodes, and unreadable files. ChangesREPL history file handling
Suggested reviewers: Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to
|
|
Updated 3:42 PM PT - Oct 21st, 2026
✅ @robobun, your commit 355126bed2fdf1581dc0bc38f18059e2e10a9f50 passed in 🧪 To try this PR locally: bunx bun-pr 38217That installs a local version of the PR into your bun-38217 --bun |
|
Status: reworked and pushed, waiting for CI. Reproduced with bun 1.4.2 on Linux, with
The first revision left the history file empty on Windows at each save (Buildkite build 94860). This revision keeps |
…as not read The first revision of this change read only the last 4 MiB of the history file. save has no limit in bytes, so it wrote files that load could not read back: one entry over 4 MiB emptied the history at the next exit. load reads the whole file again, as on main. - History::open_file leaves O_NONBLOCK out on Windows. openat returns an asynchronous handle for it there and write fails on that handle, so each save left the history file empty. - load sets file_path only after it read the file, or if the file does not exist. save then does not replace a file that load could not read. - The trim to MAX_HISTORY_SIZE entries is one drain. - Tests: two sessions, the entry cap at its boundaries, an entry over 4 MiB, a symlink, a FIFO with a reader, a device node, and a file that cannot be read.
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 Windows handle concern from the earlier review is addressed: O_NONBLOCK is now gated on cfg!(unix) in open_file, and the remaining O::NOCTTY/O::CLOEXEC bits are 0 / ignored by openat_windows, so the history handle stays synchronous there.
Beyond the inline findings, I also checked that save opening with O_TRUNC before the kind() check is harmless: the kernel ignores O_TRUNC on FIFOs and device nodes and refuses O_WRONLY on a directory, so nothing non-regular is modified before the rejection.
Extended reasoning...
The change touches only History::load/open_file/save in src/runtime/cli/repl.rs plus new history-file tests; no auth, crypto or injection surface. The earlier Windows async-handle concern was fixed by the cfg!(unix) gate, but confirmed findings remain (the PR description's tail-read/pread claim is not in the code, the unconditional O_NONBLOCK can turn a lease conflict into a silent no-history, and the exit-side FIFO guard is untested), so a human look is still warranted.
…ing a session These two tests reach the open in History::save. In the other tests of a FIFO or a device, the load fails first and save does not run.
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 finding, I also checked the cfg!(unix) gate on O_NONBLOCK in History::open_file against the Windows branch of File::kind() (GetFileType maps disk files to FileKind::File, and the handle stays synchronous for save's write_all), and confirmed the new swap-in FIFO/device tests reach save's open_file after a successful load, so the exit-side check is now exercised.
Extended reasoning...
The change touches only History::load/save/open_file in src/runtime/cli/repl.rs and the matching tests in test/js/bun/repl/repl.test.ts; it opens $HOME/.bun_repl_history with O_NONBLOCK/O_NOCTTY/O_CLOEXEC, rejects non-regular files, and leaves file_path unset when the file could not be read. No security-sensitive surface beyond file-mode handling of the user's own history file. The earlier Windows asynchronous-handle concern is addressed by the cfg!(unix) gate, and the exit-side FIFO/device cases now have tests; one inline nit about the nodev tmp mount remains, and the NFS-lease thread is still open with an author reply.
| const canMakeDevices = (() => { | ||
| if (!isLinux || !isRoot) return false; | ||
| using dir = tempDir("repl-history-device-probe", {}); | ||
| return makeNullDevice(path.join(String(dir), "null")); | ||
| })(); |
There was a problem hiding this comment.
🟡 nit (optional): on a Linux runner whose tmp is mounted nodev (systemd's default tmpfs /tmp), both root-only device tests pass on the base branch too, so they do not protect the mode fix. The probe at test/js/bun/repl/repl.test.ts:2106-2110 only checks that mknod succeeds; on a nodev mount the kernel refuses to open the node with EACCES, so the REPL never reaches fchmod on any branch and mode 0666 is kept regardless. Fix: have canMakeDevices also open the probe node for writing (openSync(device, "w")) and skip with that reason when the open fails, which covers both device tests at 2114 and 2130.
Why this was flagged
The device tests run only when isLinux and isRoot and mknod succeeds in a tempDir under os.tmpdir() (test/harness.ts:607, test/js/bun/repl/repl.test.ts:2106-2110). On a filesystem mounted with nodev, which systemd's tmp.mount uses for tmpfs /tmp, mknod as root still succeeds but open(2) of the character device returns EACCES. In that environment the REPL's open at src/runtime/cli/repl.rs:238 (and the base branch's open_a in save) fails before the kind check and before fchmod at src/runtime/cli/repl.rs:271, so statSync(device).mode stays 0o666 on the base branch as well and the assertions at 2122 and 2141 hold on both branches. The tests therefore cannot fail for the right reason there, which the repository review rules call out as a test that passes both ways. Nothing in the probe verifies that the node can actually be opened; checking that once in canMakeDevices and skipping with a reason otherwise makes the skip honest.
Verification: nit. With root on Linux and os.tmpdir() on a nodev mount, canMakeDevices at test/js/bun/repl/repl.test.ts:2106-2110 only checks mknod exits 0, but open(2) of the node fails with EACCES on a MNT_NODEV mount. On the base branch History::save returns on Err(_) before the fchmod at repl.rs:257, so both device tests (:2114, :2130) see mode 0666 and pass on base as on the PR branch.
Problem
ln -s /dev/null ~/.bun_repl_historyturns the history off. Since 1.4.0 (Hardening: input validation and protocol tightening across 24 subsystems (round 7) #31495),History::save(src/runtime/cli/repl.rs) sets the mode of what it opened to 0600. As root, that locks other users out of the device.bun replbefore it prints anything:History::loaduses a blockingopen.loadcannot read an existing file,savereplaces it at exit.Fix
History::open_fileopens withO_NONBLOCKand accepts only a regular file.loadandsaveopen through it.loadarmssaveonly after it read the file, or if no file exists.test/js/bun/repl/repl.test.ts, "REPL history file". bun 1.4.2 fails 7 of its 15 tests on Linux (Notes).Background
bun replloads~/.bun_repl_historyat startup and rewrites it at exit. No option turns that off.open(2)of a FIFO blocks until a peer opens the other end.O_NONBLOCKprevents that.openatreturns an asynchronous handle forO_NONBLOCK, soopen_fileleaves the flag out there. sys(windows): openat always opens a synchronous handle #46338 removes that mapping.savehad written (Notes).Downsides
O_NONBLOCK, an open fails at once under a file lease. That session loads or saves no history. main waits.fstatper open of the file: 11 syscalls in a saving session, 9 on bun 1.4.2.Notes
Tests, and what bun 1.4.2 does on Linux
saveis what meets the FIFO.1 + 1arrives in the FIFO./dev/null. They need root on Linux and skip elsewhere.2 + 2afterwards.remove(0)per entry over the cap is quadratic: 200,000 lines take 3.8 s on 1.4.2. A debug build of this PR loads 1,000,000 lines in 1.1 s.cargo check --workspacepasses for both Windows targets and for aarch64-apple-darwin.Windows
O_NONBLOCKon every platform. On Windows each save then left the history file empty: Buildkite build 94860, both Windows lanes, three tests.open_filenow leaves the flag out withcfg!(unix), asscript_or_command.rsandlint/driver/fs.rsdo. The arm can go after sys(windows): openat always opens a synchronous handle #46338.Why the 4 MiB limit is gone
savehas no limit in bytes, so it wrote files thatloadcould not read back. With a newest entry of 5 MiB after 300 short ones, the next session listed no history and left 1 line of 301. bun 1.4.2 keeps all of them.savekeeps 1000 entries.File leases
open(2)returnsEWOULDBLOCKifO_NONBLOCKis set and another process holds an incompatible lease (a Samba oplock, an NFS delegation).loadfails, so the session has no history and saves none. Under a read leasesavefails, so the new entries are lost. Nothing that is in the file is lost.Left on purpose
.load <file>and.save <file>open a path that the user typed. They are not changed.bun --interactivehas its own history code (src/js/internal/repl/history.js, the port of Node's). It is not changed.fchmodafter an open in place, are not changed.Self-review, the 4 concerns that are not done
O_NONBLOCKwhen the open fails under a lease. It needs a second open, and a test that holds a lease through FFI. Nothing in the file is lost without the retry.fchmodafter an open in place. Other open PRs own them.Other changes in
loaddrain, not oneremove(0)per entry. repl: append each history entry to the history file when it is accepted #46109 has the same two lines.Other open PRs
savewith a writer that appends. The one of the two that lands second opens throughopen_file.load. This PR leaves that loop as it is on main.File::open_regular_attobun_sys.History::open_fileis the same check for this one file, and can call the shared one after that lands..bun_repl_historyare about where the file is.