Skip to content
7 changes: 7 additions & 0 deletions src/js/internal/fs/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@
function writeFast(this: FSStream, data: any, encoding: any, cb: any) {
if (this[kWriteMonkeyPatchDefense]) return writablePrototypeWrite.$call(this, data, encoding, cb);

// After end()/destroy()/error the Writable contract requires write() to fail
// (ERR_STREAM_WRITE_AFTER_END / ERR_STREAM_DESTROYED) and not reach the sink.
const state = this._writableState;
if (state !== undefined && (state.ending || state.ended || state.destroyed || state.errored)) {

Check warning on line 666 in src/js/internal/fs/streams.ts

View check run for this annotation

Claude / Claude Code Review

destroy()/errored guard clauses are untested

The guard checks four conditions (`ending || ended || destroyed || errored`), but the new test only exercises `end()`. The `destroyed` and `errored` branches — which the PR description explicitly claims to fix ('or after destroy()') and which produce `ERR_STREAM_DESTROYED` — have no coverage; deleting `|| state.destroyed || state.errored` would not break any test. Consider a sibling fixture variant that calls `so.destroy()` before writing and asserts `ERR_STREAM_DESTROYED`.
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
return writablePrototypeWrite.$call(this, data, encoding, cb);
}

if (typeof encoding === "function") {
cb = encoding;
encoding = undefined;
Expand Down
25 changes: 25 additions & 0 deletions test/js/node/process/process-stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,29 @@
`hello worldhello again|😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌`.repeat(9999),
);
});

test("process.stdout - write after end() errors and is not delivered (piped)", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), path.join(import.meta.dir, "process-stdout-write-after-end-fixture.mjs")],
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env: bunEnv,
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

// The "C" write after end() must not reach the pipe reader.
expect(stdout).toBe("AB");

const report = JSON.parse(stderr.trim());

Check warning on line 176 in test/js/node/process/process-stdio.test.ts

View check run for this annotation

Claude / Claude Code Review

JSON.parse on raw stderr is brittle to benign warnings

nit: `JSON.parse(stderr.trim())` assumes stderr contains nothing but the JSON line, which is fragile if a debug/ASAN build ever emits a benign warning to stderr (per the Landing PRs guidance on not asserting stderr is exactly empty). Since stdout is unavailable here, consider extracting the JSON line before parsing — e.g. `JSON.parse(stderr.split("\n").find(l => l.startsWith("{")))`.
expect(report).toEqual({
writableEnded: true,
writable: false,
ret: false,
cbErr: "ERR_STREAM_WRITE_AFTER_END",
ev: ["err:ERR_STREAM_WRITE_AFTER_END"],
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
expect(exitCode).toBe(0);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});
26 changes: 26 additions & 0 deletions test/js/node/process/process-stdout-write-after-end-fixture.mjs

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

Loading