Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/runtime/webcore/Blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5047,12 +5047,13 @@
#[cfg(not(windows))]
{
let mut needs_async = false;
// The fast path writes synchronously on the JS thread. Routing an fd through the thread
// pool instead would drain the pool's LIFO run queue out of submission order, so fd
// destinations always stay on this path; the blocking-pipe case falls through to the
// async path via `needs_async` on EAGAIN.
let fast_path_ok = matches!(*path_or_blob, PathOrBlob::Path(_))
|| (matches!(*path_or_blob, PathOrBlob::Blob(ref b)
if b.offset.get() == 0 && !b.is_s3()
&& !(b.store.get().is_some()
&& matches!(b.store().expect("infallible: store present").data, store::Data::File(ref f)
if f.mode != 0 && bun_core::kind_from_mode(f.mode) == bun_core::FileKind::File))));
|| matches!(*path_or_blob, PathOrBlob::Blob(ref b)
if b.offset.get() == 0 && !b.is_s3());

Check failure on line 5056 in src/runtime/webcore/Blob.rs

View check run for this annotation

Claude / Claude Code Review

Empty Bun.write to file-redirected stdout/stderr now ftruncates the output file

Routing regular-file fd blobs into the sync fast path exposes them to the `truncate = NEEDS_OPEN || input.is_empty()` clause at Blob.rs:5423/:5507 — with `NEEDS_OPEN=false` and an empty payload, this now issues `ftruncate(fd, 0)` on a regular file (where it *succeeds*, unlike the pipe/tty case that harmlessly EINVALs). So `Bun.write(Bun.stdout, "")` under `> out.txt` now wipes everything already written, whereas before this PR it went through the async `WriteFileTask` path which never truncates.
Comment thread
robobun marked this conversation as resolved.
Resolved
if fast_path_ok {
if data.is_string() {
let len = data.get_length(global_this)?;
Expand Down
50 changes: 50 additions & 0 deletions test/js/bun/io/bun-write.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,54 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) {

expect(f.name).toBe(filePath);
});

// On Windows all Bun.write() goes through uv_fs_write on the libuv thread pool, which has its
// own (different) reordering behaviour; this covers the POSIX synchronous fast path.
describe.skipIf(isWindows).each(["stdout", "stderr"])("Bun.write(Bun.%s, ...) ordering", stream => {
const otherStream = stream === "stdout" ? "stderr" : "stdout";
const script = `
const p = [];
for (let i = 0; i < 5; i++) p.push(Bun.write(Bun.${stream}, "C" + i + "\\n"));
const pending = p.filter(x => Bun.peek.status(x) === "pending").length;
await Promise.all(p);
process.${otherStream}.write("pending=" + pending + "\\n");
`;

it("writes in call order when redirected to a file", async () => {
using dir = tempDir("bun-write-stdio-order-file", {});
const outPath = join(String(dir), "out.txt");
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: bunEnv,
stdout: stream === "stdout" ? Bun.file(outPath) : "pipe",
stderr: stream === "stderr" ? Bun.file(outPath) : "pipe",
});
const other = stream === "stdout" ? proc.stderr : proc.stdout;
const [otherText, exitCode] = await Promise.all([other.text(), proc.exited]);
// Small writes to a regular-file stdio fd take the same synchronous path as pipes, so they
// land in call order and their promises are already settled when Bun.write() returns.
// Previously they were dispatched to the thread pool, whose LIFO run queue reversed them.
expect({ output: await Bun.file(outPath).text(), status: otherText }).toEqual({
output: "C0\nC1\nC2\nC3\nC4\n",
status: "pending=0\n",
});
expect(exitCode).toBe(0);
});

it("writes in call order when redirected to a pipe", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
const [target, other] = stream === "stdout" ? [stdout, stderr] : [stderr, stdout];
expect({ output: target, status: other }).toEqual({
output: "C0\nC1\nC2\nC3\nC4\n",
status: "pending=0\n",
});
expect(exitCode).toBe(0);
});
});
});
Loading