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
6 changes: 3 additions & 3 deletions src/js/internal/fs/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,10 @@ const kWriteMonkeyPatchDefense = Symbol("!");
function writeFast(this: FSStream, data: any, encoding: any, cb: any) {
if (this[kWriteMonkeyPatchDefense]) return writablePrototypeWrite.$call(this, data, encoding, cb);

// After end() the Writable contract requires write() to fail with
// ERR_STREAM_WRITE_AFTER_END and not reach the sink.
// After end()/destroy() the Writable contract requires write() to fail with
// ERR_STREAM_WRITE_AFTER_END / ERR_STREAM_DESTROYED and not reach the sink.
const state = this._writableState;
if (state !== undefined && state.ending) {
if (state !== undefined && (state.ending || state.destroyed)) {
return writablePrototypeWrite.$call(this, data, encoding, cb);
}

Expand Down
49 changes: 49 additions & 0 deletions test/js/node/child_process/child-process-stdio.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { execSync, spawn } from "node:child_process";
import { once } from "node:events";

const CHILD_PROCESS_FILE = import.meta.dir + "/spawned-child.js";
const OUT_FILE = import.meta.dir + "/stdio-test-out.txt";
Expand Down Expand Up @@ -117,3 +118,51 @@ describe("process.stdin", () => {
expect(result).toEqual("data: File read successfully");
});
});

describe("child.stdin", () => {
it("write() after child 'close' returns false and calls back with ERR_STREAM_DESTROYED", async () => {
const child = spawn(bunExe(), ["-e", ""], {
env: bunEnv,
stdio: ["pipe", "ignore", "ignore"],
});
await once(child, "close");

const { promise, resolve } = Promise.withResolvers();
const ret = child.stdin.write("dropped", resolve);
const cbErr = await promise;

expect({
ret,
cbCode: cbErr?.code,
destroyed: child.stdin.destroyed,
writable: child.stdin.writable,
}).toEqual({
ret: false,
cbCode: "ERR_STREAM_DESTROYED",
destroyed: true,
writable: false,
});
});

it("write() after child 'exit' (before 'close') returns false and calls back with ERR_STREAM_DESTROYED", async () => {
const child = spawn(bunExe(), ["-e", "process.stdin.once('data', () => process.exit(0))"], {
env: bunEnv,
stdio: ["pipe", "ignore", "ignore"],
});
child.stdin.on("error", () => {});
await new Promise((resolve, reject) => {
child.once("error", reject);
child.stdin.write("go\n", err => (err ? reject(err) : resolve()));
});
await once(child, "exit");

const { promise, resolve } = Promise.withResolvers();
const ret = child.stdin.write("late", resolve);
const cbErr = await promise;

expect({ ret, cbCode: cbErr?.code }).toEqual({
ret: false,
cbCode: "ERR_STREAM_DESTROYED",
});
});
});
Loading