Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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

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 @@
expect(exitCode).toBe(0);
},
);

test.concurrent.each(["stdout", "stderr"] as const)(
"process.%s - write after end() succeeds and is delivered (file)",

Check failure on line 48 in test/js/node/process/process-stdout-write-after-end.test.ts

View check run for this annotation

Claude / Claude Code Review

Follow-up commit reverted the source fix; PR now contains only tests

Commit 7664448e (the `test.concurrent.each` follow-up) accidentally deleted the source fix from `src/js/builtins/ProcessObjectInternals.ts` — the net PR diff now contains only the two test files and zero source changes. Without the `else { stream._writableState.autoDestroy = true; }` branch in `getStdioWriteStream`, the new file-backed tests will fail with `ERR_STREAM_WRITE_AFTER_END`. Restore the deleted else-block before merging.
Comment thread
robobun marked this conversation as resolved.
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