Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/js/builtins/ProcessObjectInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export function getStdioWriteStream(
// stdout/stderr don't produce readable data, so yield nothing
})();
};
} else {
// File-backed stdio: Node's SyncWriteStream runs end() -> finish ->
// destroy -> the _destroy override below -> _undestroy(), which resets
// writable state so later writes succeed. autoClose:false disabled that.
stream._writableState.autoDestroy = true;
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 50 additions & 2 deletions test/js/node/process/process-stdout-write-after-end.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tempDir } from "harness";
import fs from "node:fs";
import path from "path";

test.each(["stdout", "stderr"] as const)(
test.concurrent.each(["stdout", "stderr"] as const)(
"process.%s - write after end() errors and is not delivered (piped)",
async which => {
await using proc = Bun.spawn({
Expand Down Expand Up @@ -42,3 +43,50 @@ test.each(["stdout", "stderr"] as const)(
expect(exitCode).toBe(0);
},
);

test.concurrent.each(["stdout", "stderr"] as const)(
"process.%s - write after end() succeeds and is delivered (file)",
Comment thread
robobun marked this conversation as resolved.
async which => {
using dir = tempDir("stdio-write-after-end-file", {});
const outPath = path.join(String(dir), "out.txt");
const fd = fs.openSync(outPath, "w");
try {
// Redirect the fixture's target stream to a regular file; the report
// stream stays piped so we can read the JSON facts.
await using proc = Bun.spawn({
cmd: [bunExe(), path.join(import.meta.dir, "process-stdout-write-after-end-file-fixture.mjs"), which],
stdout: which === "stdout" ? fd : "pipe",
stderr: which === "stderr" ? fd : "pipe",
stdin: "ignore",
env: bunEnv,
});

const reportStream = which === "stderr" ? proc.stdout : proc.stderr;
const [reportText, exitCode] = await Promise.all([reportStream.text(), proc.exited]);
const lines = reportText.trim().split("\n");
const report = JSON.parse(lines[lines.length - 1]);

// Node's file-backed stdio is never-closing: end() runs the finish ->
// destroy -> _undestroy cycle, which resets writable state, so a later
// write() succeeds with no error and writableEnded is false again.
expect(report).toEqual({
writableEnded: false,
writable: true,
ret: true,
cbErr: null,
ev: [],
});

const fileContents = fs.readFileSync(outPath, "latin1");
// stderr may carry benign ASAN/debug noise on fd 2; stdout is clean.
if (which === "stdout") {
expect(fileContents).toBe("ABCD\n");
} else {
expect(fileContents).toContain("ABCD\n");
}
expect(exitCode).toBe(0);
} finally {
fs.closeSync(fd);
}
},
);
Loading