stream: fix pipeTo assert when writer is released during microtask

pipeTo defers each slow-path write to a microtask so it does not run
synchronously during enqueue(). A race allows the pipe to shut down and
release its writer after the write is scheduled but before it runs. The
deferred write then hits an ERR_INTERNAL_ASSERTION because
writer[[stream]] is undefined.

Skip the deferred write when shutdown has already started.

Fixes: https://github.com/nodejs/node/issues/63732
Signed-off-by: Qingyu Wang <wangqingyu.c0l1n@bytedance.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/63733
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Qingyu Wang authored and Antoine du Hamel committed 2026-11-11 17:20:31 +01:00
1 parent 8163152f5d
commit 58eb15cd6b
2 files changed
+64

No files matched your search

@@ -1792,6 +1792,7 @@ function readableStreamPipeTo(
function forwardChunk() {
const chunk = pendingChunk;
pendingChunk = undefined;
if (writer[kState].stream === undefined) return;
writableStreamDefaultWriterWriteWithRequest(writer, chunk, writeTracker);
pump();
}
@@ -0,0 +1,63 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { ReadableStream, WritableStream } = require('stream/web');
{
let sourceController;
let destController;
const source = new ReadableStream({
start(controller) {
sourceController = controller;
},
});
const dest = new WritableStream({
start(controller) {
destController = controller;
},
write() {},
});
assert.rejects(
source.pipeTo(dest, { preventCancel: true }),
{ message: 'destination errored' },
).then(common.mustCall());
setImmediate(common.mustCall(() => {
destController.error(new Error('destination errored'));
sourceController.enqueue('chunk');
}));
}
{
const ac = new AbortController();
let sourceController;
const chunks = [];
const source = new ReadableStream({
start(controller) {
sourceController = controller;
},
}, { highWaterMark: 0 });
const dest = new WritableStream({
write: common.mustCall((chunk) => {
chunks.push(chunk);
}),
}, { highWaterMark: 1 });
assert.rejects(
source.pipeTo(dest, { signal: ac.signal }),
{ name: 'AbortError' },
).then(common.mustCall(() => {
assert.deepStrictEqual(chunks, ['chunk']);
}));
setImmediate(common.mustCall(() => {
sourceController.enqueue('chunk');
ac.abort();
}));
}