mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
stream: observe abort while awaiting pipeTo source
Use the abort-aware iterator wrapper in the no-transform pipeTo() path so a pending source read does not block AbortSignal handling. Fixes: https://github.com/nodejs/node/issues/64014 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: https://github.com/nodejs/node/pull/64015 Fixes: https://github.com/nodejs/node/issues/64014 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
1 parent
f7adcd8359
commit
d0b830b382
2 files changed
+35
-1
No files matched your search
@@ -1090,7 +1090,7 @@ async function pipeTo(source, ...args) {
|
||||
} else if (transforms.length === 0) {
|
||||
// Fast path: no transforms - iterate normalized source directly
|
||||
if (signal) {
|
||||
for await (const batch of normalized) {
|
||||
for await (const batch of yieldAbortable(normalized, signal)) {
|
||||
signal.throwIfAborted();
|
||||
const p = writeBatch(batch);
|
||||
if (p) await p;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { setTimeout } = require('timers/promises');
|
||||
const { pipeTo, from } = require('stream/iter');
|
||||
|
||||
// pipeTo with live signal, no transforms — abort mid-stream
|
||||
@@ -30,6 +31,38 @@ async function testPipeToLiveSignalNoTransforms() {
|
||||
assert.ok(written.length >= 1);
|
||||
}
|
||||
|
||||
// pipeTo with live signal, no transforms — abort while waiting for next chunk
|
||||
async function testPipeToLiveSignalNoTransformsPendingNext() {
|
||||
const ac = new AbortController();
|
||||
const reason = new Error('abort reason');
|
||||
const writer = {
|
||||
write: common.mustNotCall(),
|
||||
};
|
||||
const source = {
|
||||
[Symbol.asyncIterator]() {
|
||||
return {
|
||||
next() {
|
||||
return new Promise(() => {});
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
setTimeout(10)
|
||||
.then(() => ac.abort(reason))
|
||||
.then(common.mustCall());
|
||||
|
||||
const result = await Promise.race([
|
||||
assert.rejects(
|
||||
() => pipeTo(source, writer, { signal: ac.signal }),
|
||||
reason,
|
||||
).then(() => 'aborted'),
|
||||
setTimeout(1000, 'timed out'),
|
||||
]);
|
||||
|
||||
assert.strictEqual(result, 'aborted');
|
||||
}
|
||||
|
||||
// pipeTo with live signal + transforms — abort mid-stream
|
||||
async function testPipeToLiveSignalWithTransforms() {
|
||||
const ac = new AbortController();
|
||||
@@ -84,6 +117,7 @@ async function testPipeToLiveSignalWithTransformsCompletes() {
|
||||
|
||||
Promise.all([
|
||||
testPipeToLiveSignalNoTransforms(),
|
||||
testPipeToLiveSignalNoTransformsPendingNext(),
|
||||
testPipeToLiveSignalWithTransforms(),
|
||||
testPipeToLiveSignalCompletes(),
|
||||
testPipeToLiveSignalWithTransformsCompletes(),
|
||||
|
||||
Reference in new issue
Block a user