diff --git a/cli/tests/unit_node/process_test.ts b/cli/tests/unit_node/process_test.ts index 49b753db3a..7e927a8ad2 100644 --- a/cli/tests/unit_node/process_test.ts +++ b/cli/tests/unit_node/process_test.ts @@ -2,6 +2,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import process, { argv, env } from "node:process"; +import { Readable } from "node:stream"; +import { once } from "node:events"; import { assert, assertEquals, @@ -746,3 +748,19 @@ Deno.test({ assertEquals(stripColor(decoder.decode(stdout).trim()), "really exited"); }, }); + +Deno.test({ + name: "process.stdout isn't closed when source stream ended", + async fn() { + const source = Readable.from(["foo", "bar"]); + + source.pipe(process.stdout); + await once(source, "end"); + + // Wait a bit to ensure that streaming is completely finished. + await delay(10); + + // This checks if the rid 1 is still valid. + assert(typeof process.stdout.isTTY === "boolean"); + }, +}); diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs index 9096586c7f..934d4f9670 100644 --- a/ext/node/polyfills/_process/streams.mjs +++ b/ext/node/polyfills/_process/streams.mjs @@ -16,6 +16,7 @@ import * as io from "ext:deno_io/12_io.js"; // https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41 export function createWritableStdioStream(writer, name) { const stream = new Writable({ + emitClose: false, write(buf, enc, cb) { if (!writer) { this.destroy(