stream: avoid a promise per stream/iter from() read in pipelines

A pull() pipeline, pipeTo() with a signal and the consumers with a
signal reject their own pending pull, pipe or read when they are
aborted, and ignore the read of the source that is still pending then.
Yet they read a from() async source with its cancellable next(), which
gives every read a promise of its own so that a cancellation can reject
it at once: about 230 bytes per batch, for a rejection nobody waits for.

They now read such sources with kNextUncancellable, as pipeTo() without
a signal already did, when nothing but stream/iter code waits for the
reads: always for pipeTo() and the consumers, and for pull() pipelines
without stateful transforms. A stateful transform's generator waits for
the source in user code, so with one the reads stay cancellable and its
finally block still runs at once.

What makes this possible is that a cancellation now releases a pending
kNextUncancellable read instead of waiting for it: the normalization
ends, the source is closed at once (as for a cancelled next(), errors
ignored), and the operations queued behind the read, such as the
return() that cancelled it, run. The read settles when the source's
next() does, rejecting with the cancellation reason, and is ignored. A
pipeline stopped with return() or throw() while a pull is pending closes
its (stateless) transform layers without waiting for them, since they
wait for that read.

Allocation per batch, 16-byte chunks, async source:
- pipeTo() with a signal: 967 -> 738 bytes (as without a signal)
- pull() with one transform: 1518 -> 1289 bytes
- pipeTo() with a signal and one transform: 1829 -> 1601 bytes

test-stream-iter-abort-pending-read.js checks, for pull(), pipeTo() and
bytes(), with and without transforms and stopped by a signal, return()
or throw() while the source's next() is pending, that the call rejects
at once, the source is closed before that next() settles, and nothing
happens when it does. It also passes before this change.

Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell committed 2026-11-22 08:27:34 -08:00
1 parent a8faad05df
commit e372bb1dca
4 files changed
+286 -22

No files matched your search

+7 -1
View File
@@ -55,6 +55,7 @@ const {
fromSync, fromSync,
isAsyncIterable, isAsyncIterable,
isSyncIterable, isSyncIterable,
kNextUncancellable,
} = require('internal/streams/iter/from'); } = require('internal/streams/iter/from');
const { const {
@@ -171,10 +172,15 @@ async function collectAsync(source, signal, limit) {
// Read as for await...of does, stopping once the signal aborts; see // Read as for await...of does, stopping once the signal aborts; see
// raceSignal(). // raceSignal().
// A from() source is read with kNextUncancellable: on an abort,
// raceSignal() rejects and closes the source at once, and a read still
// pending is ignored.
const state = { __proto__: null, aborted: false }; const state = { __proto__: null, aborted: false };
async function consume(iterator) { async function consume(iterator) {
const uncancellable = iterator[kNextUncancellable] !== undefined;
for (;;) { for (;;) {
const result = await iterator.next(); const result = await (uncancellable ?
iterator[kNextUncancellable]() : iterator.next());
if (result.done || state.aborted) return; if (result.done || state.aborted) return;
try { try {
recordBatch(result.value); recordBatch(result.value);
+51 -13
View File
@@ -152,8 +152,13 @@ function waitForNormalization(value, context) {
const kNextSyncBatch = Symbol('kNextSyncBatch'); const kNextSyncBatch = Symbol('kNextSyncBatch');
// The method of an iterator returned by from() for an async iterable that // The method of an iterator returned by from() for an async iterable that
// reads like next() when nothing can cancel the normalization while a read // reads like next(), for internal callers that stop reading when they
// is pending, for pipeTo(): see createAsyncSourceNormalizer(). // cancel the normalization (pipeTo(), pull() pipelines, consumers): a
// return() or throw() while a read is pending closes the source at once,
// as for next(), but does not settle the pending read early. That read
// settles when the source's next() does, rejecting with the cancellation
// reason, and the caller ignores it. It saves the read a promise of its
// own: see createAsyncSourceNormalizer().
const kNextUncancellable = Symbol('kNextUncancellable'); const kNextUncancellable = Symbol('kNextUncancellable');
// Methods of the iterator returned by yieldNormalizationAbortable(), used by // Methods of the iterator returned by yieldNormalizationAbortable(), used by
@@ -591,11 +596,12 @@ function yieldNormalizationAbortable(source, context) {
}); });
return promise; return promise;
}, },
// Like next(), when nothing cancels the normalization while the read // Like next(), for a caller that ignores the read if it cancels the
// is pending, in two parts so that the caller handles the result in // normalization while the read is pending (see kNextUncancellable),
// the same reaction: kReadUncancellable starts the read, returning a // in two parts so that the caller handles the result in the same
// promise for the source's result, and the caller passes that result // reaction: kReadUncancellable starts the read, returning a promise
// to kToIterResult, or calls kReadFailed if the promise rejects. // for the source's result, and the caller passes that result to
// kToIterResult, or calls kReadFailed if the promise rejects.
[kReadUncancellable]() { [kReadUncancellable]() {
if (completed) { if (completed) {
return PromiseResolve(kDoneSourceResult); return PromiseResolve(kDoneSourceResult);
@@ -836,9 +842,11 @@ function createAsyncSourceNormalizer(source, context) {
let boundedBatches = null; let boundedBatches = null;
let valueBatches = null; let valueBatches = null;
// Whether the source is read with kNextUncancellable: set by the // Whether the source is read with kNextUncancellable: set by the
// kNextUncancellable method, for callers that never cancel the // kNextUncancellable method, for callers that ignore a read pending when
// normalization while a read is pending. // they cancel the normalization. readReleased: whether a cancellation has
// released such a read (see onUncancellableCancelled()).
let uncancellable = false; let uncancellable = false;
let readReleased = false;
// Whether the source is read with kRead, and the settling functions of // Whether the source is read with kRead, and the settling functions of
// the read's promise while it is pending. // the read's promise while it is pending.
let readable = false; let readable = false;
@@ -915,6 +923,8 @@ function createAsyncSourceNormalizer(source, context) {
// A kReadUncancellable result: handled as the result of the source's // A kReadUncancellable result: handled as the result of the source's
// next() is, in the same reaction. // next() is, in the same reaction.
function onUncancellableResult(result) { function onUncancellableResult(result) {
if (readReleased) throw context.reason;
context.resolve = null;
let iterResult; let iterResult;
try { try {
iterResult = iterator[kToIterResult](result); iterResult = iterator[kToIterResult](result);
@@ -925,10 +935,41 @@ function createAsyncSourceNormalizer(source, context) {
} }
function onUncancellableError(error) { function onUncancellableError(error) {
if (readReleased) throw context.reason;
context.resolve = null;
iterator[kReadFailed](); iterator[kReadFailed]();
return fail(error); return fail(error);
} }
// context.resolve while a kNextUncancellable read is pending: the caller
// ignores that read, so it is released rather than waited for. The
// normalization ends, the source is closed at once, as for a cancelled
// kRead (errors ignored), and the operations queued behind the read,
// such as the return() that cancelled it, run now. The read settles when
// the source's next() does, rejecting with the cancellation reason.
function onUncancellableCancelled() {
context.resolve = null;
readReleased = true;
state = kDone;
markPromiseAsHandled(iterator.return());
settled();
}
function readUncancellable() {
const cancelledBefore = context.cancelled;
const read = iterator[kReadUncancellable]();
if (!cancelledBefore) {
// Cancelled while the source's next() was called, or later.
if (context.cancelled) {
onUncancellableCancelled();
} else {
context.resolve = onUncancellableCancelled;
}
}
return PromisePrototypeThen(read, onUncancellableResult,
onUncancellableError);
}
// The kRead handlers: the result is handled in the reaction to the // The kRead handlers: the result is handled in the reaction to the
// source's result, and settles the read's promise. // source's result, and settles the read's promise.
function onRead(result) { function onRead(result) {
@@ -954,10 +995,7 @@ function createAsyncSourceNormalizer(source, context) {
} }
function pullSource() { function pullSource() {
if (uncancellable) { if (uncancellable) return readUncancellable();
return PromisePrototypeThen(iterator[kReadUncancellable](),
onUncancellableResult, onUncancellableError);
}
if (readable) { if (readable) {
// One promise for the read, which a cancellation rejects, instead of // One promise for the read, which a cancellation rejects, instead of
// next()'s promise and a reaction to it. // next()'s promise and a reaction to it.
+63 -8
View File
@@ -1233,20 +1233,44 @@ class PipelineSource {
#abort; #abort;
#iterator = null; #iterator = null;
#closed = false; #closed = false;
// Whether a from() source is read with kNextUncancellable (see the
// constructor).
#uncancellable;
constructor(source, abort) { // `uncancellable`: whether nothing but stream/iter code waits for the
// reads, so that a read pending when the pipeline aborts can be left to
// settle when the source's next() does. The pipeline rejects its own
// pending pull and ignores the read, and closing the source still calls
// its return() at once. This saves every read of a from() source a
// promise of its own. A stateful transform reads the source in user code
// (its generator waits for the read), so with one the reads stay
// cancellable, and an abort rejects them at once.
constructor(source, abort, uncancellable) {
this.#source = source; this.#source = source;
this.#abort = abort; this.#abort = abort;
this.#uncancellable = uncancellable;
} }
[SymbolAsyncIterator]() { [SymbolAsyncIterator]() {
return this; return this;
} }
// Whether the source is read with kNextUncancellable (known once it has
// been read).
get uncancellable() {
return this.#uncancellable && this.#iterator !== null;
}
next() { next() {
if (this.#abort.aborted) return PromiseReject(this.#abort.reason); if (this.#abort.aborted) return PromiseReject(this.#abort.reason);
if (this.#closed) return PromiseResolve(new IterResult(true, undefined)); if (this.#closed) return PromiseResolve(new IterResult(true, undefined));
this.#iterator ??= this.#source[SymbolAsyncIterator](); if (this.#iterator === null) {
this.#iterator = this.#source[SymbolAsyncIterator]();
if (this.#iterator[kNextUncancellable] === undefined) {
this.#uncancellable = false;
}
}
if (this.#uncancellable) return this.#iterator[kNextUncancellable]();
return this.#iterator.next(); return this.#iterator.next();
} }
@@ -1290,6 +1314,13 @@ class PipelineSource {
} }
} }
function hasTransformObject(transforms) {
for (let i = 0; i < transforms.length; i++) {
if (isTransformObject(transforms[i])) return true;
}
return false;
}
/** /**
* Build the chain of transform layers of a pipeline. * Build the chain of transform layers of a pipeline.
* @param {AsyncIterable<Uint8Array[]>} normalized * @param {AsyncIterable<Uint8Array[]>} normalized
@@ -1377,6 +1408,8 @@ function createAsyncPipeline(source, transforms, signal,
// runs one pull at a time. // runs one pull at a time.
let resolvePull = null; let resolvePull = null;
let rejectPull = null; let rejectPull = null;
// See stop().
let closeWithoutWaiting = false;
const operations = createOperationQueue(); const operations = createOperationQueue();
const self = ObjectSetPrototypeOf({ const self = ObjectSetPrototypeOf({
@@ -1437,6 +1470,14 @@ function createAsyncPipeline(source, transforms, signal,
if (rejectPull !== null) { if (rejectPull !== null) {
abortPipeline(reason, false); abortPipeline(reason, false);
state = kActive; state = kActive;
// The pending pull is waiting for a read of the source that is now
// ignored, and with uncancellable reads nothing settles that read
// early: closing the transforms would wait for it, as a transform
// layer's return() waits for its pending next(). The transforms are
// all stateless then (see PipelineSource), so nothing but stream/iter
// code waits for them, and doReturn() / doThrow() close them without
// waiting. The source has already been closed.
closeWithoutWaiting = pipelineSource.uncancellable;
} else { } else {
abort.abort(reason); abort.abort(reason);
} }
@@ -1542,7 +1583,8 @@ function createAsyncPipeline(source, transforms, signal,
if (state === kStart) { if (state === kStart) {
state = kActive; state = kActive;
abort = new PipelineAbort(); abort = new PipelineAbort();
pipelineSource = new PipelineSource(source, abort); pipelineSource = new PipelineSource(
source, abort, !hasTransformObject(transforms));
if (signal !== undefined) { if (signal !== undefined) {
signal.addEventListener('abort', onSignalAbort, signal.addEventListener('abort', onSignalAbort,
{ __proto__: null, once: true, { __proto__: null, once: true,
@@ -1600,6 +1642,11 @@ function createAsyncPipeline(source, transforms, signal,
} }
state = kDone; state = kDone;
removeSignalListener(); removeSignalListener();
if (closeWithoutWaiting) {
markPromiseAsHandled(closeAsyncIterator(iterator, false));
operations.settled();
return PromiseResolve(result);
}
return PromisePrototypeThen( return PromisePrototypeThen(
closeAsyncIterator(iterator, false), () => { closeAsyncIterator(iterator, false), () => {
operations.settled(); operations.settled();
@@ -1618,6 +1665,11 @@ function createAsyncPipeline(source, transforms, signal,
} }
state = kDone; state = kDone;
removeSignalListener(); removeSignalListener();
if (closeWithoutWaiting) {
markPromiseAsHandled(closeAsyncIterator(iterator, true));
operations.settled();
return PromiseReject(error);
}
return PromisePrototypeThen( return PromisePrototypeThen(
closeAsyncIterator(iterator, true), () => { closeAsyncIterator(iterator, true), () => {
operations.settled(); operations.settled();
@@ -2040,10 +2092,14 @@ async function pipeTo(source, ...args) {
} }
// Write the batches of a source read with next(), as the for await...of // Write the batches of a source read with next(), as the for await...of
// loop below does, stopping once `signal` aborts. // loop below does, stopping once `signal` aborts. A from() source is read
// with kNextUncancellable: on an abort, raceSignal() rejects and closes
// the source at once, and a read still pending is ignored.
async function pipeSourceWithSignal(iterator) { async function pipeSourceWithSignal(iterator) {
const uncancellable = iterator[kNextUncancellable] !== undefined;
for (;;) { for (;;) {
const result = await iterator.next(); const result = await (uncancellable ?
iterator[kNextUncancellable]() : iterator.next());
if (result.done || abortState.aborted) return; if (result.done || abortState.aborted) return;
try { try {
const p = writeBatch(result.value); const p = writeBatch(result.value);
@@ -2144,9 +2200,8 @@ async function pipeTo(source, ...args) {
if (transforms.length === 0) { if (transforms.length === 0) {
// Fast path: no transforms - iterate normalized source directly // Fast path: no transforms - iterate normalized source directly
if (signal) { if (signal) {
// Batches are read synchronously when possible, as without a signal. // Batches are read synchronously when possible, as without a signal,
// Async reads go through next(), which return() can cancel while it // otherwise as pipeSourceWithSignal() says.
// is pending, unlike kNextUncancellable.
const iterator = normalized[SymbolAsyncIterator](); const iterator = normalized[SymbolAsyncIterator]();
await raceSignal(iterator, signal, abortState, await raceSignal(iterator, signal, abortState,
iterator[kNextSyncBatch] !== undefined ? iterator[kNextSyncBatch] !== undefined ?
@@ -0,0 +1,165 @@
// Flags: --experimental-stream-iter
'use strict';
// An abort while a from() source's next() is pending: the pull, pipe or
// consumer rejects at once with the abort reason, and the source is closed
// at once, before its pending next() settles. When that next() settles
// later, nothing more happens: no write, no batch delivered, and no
// unhandled rejection (which would fail the test).
const common = require('../common');
const assert = require('assert');
const { bytes, pipeTo, pull, from } = require('stream/iter');
const tick = () => new Promise(setImmediate);
// An async source whose first next() gives one batch and whose later
// next() calls stay pending until release().
function pendingSource() {
const log = [];
let reads = 0;
let release = null;
const source = {
[Symbol.asyncIterator]() { return this; },
next() {
reads++;
log.push('next');
if (reads === 1) {
return Promise.resolve({ done: false, value: [new Uint8Array([1])] });
}
return new Promise((resolve) => {
release = () => resolve({ done: false, value: [new Uint8Array([2])] });
});
},
return() {
log.push('return');
return Promise.resolve({ done: true, value: undefined });
},
};
return { source, log, release: () => release() };
}
async function checkPendingRead(start, { stateful = false } = {}) {
const { source, log, release } = pendingSource();
const ac = new AbortController();
const reason = new Error('stop');
const { done, readFirst, extra } = start(from(source), ac.signal, log);
await readFirst;
await tick();
assert.deepStrictEqual(log, ['next', 'next']);
ac.abort(reason);
await assert.rejects(done, reason);
await tick();
// Closed while its next() is pending.
assert.deepStrictEqual(log.slice(0, 3), ['next', 'next', 'return']);
if (stateful) assert.ok(log.includes('finally'));
release();
await tick();
await tick();
assert.deepStrictEqual(extra(), []);
assert.strictEqual(log.filter((x) => x === 'return').length, 1);
assert.strictEqual(log.filter((x) => x === 'next').length, 2);
}
// pull() with a signal, with and without a stateless transform.
async function testPull(transforms) {
await checkPendingRead((normalized, signal) => {
const it = pull(normalized, ...transforms, { signal })[Symbol.asyncIterator]();
const late = [];
const readFirst = it.next();
const done = readFirst.then(() => it.next()).then((r) => late.push(r));
return { done, readFirst, extra: () => late };
});
}
// pull() without a signal, stopped with return() while a pull is pending.
async function testPullReturn() {
const { source, log, release } = pendingSource();
const it = pull(from(source), (c) => c)[Symbol.asyncIterator]();
await it.next();
const pending = it.next();
await tick();
const returned = it.return();
await assert.rejects(pending, { name: 'AbortError' });
await returned;
assert.deepStrictEqual(log, ['next', 'next', 'return']);
release();
await tick();
await tick();
assert.deepStrictEqual(log, ['next', 'next', 'return']);
}
// The same, stopped with throw().
async function testPullThrow() {
const { source, log, release } = pendingSource();
const it = pull(from(source), (c) => c)[Symbol.asyncIterator]();
await it.next();
const pending = it.next();
await tick();
const error = new Error('thrown');
const thrown = it.throw(error);
await assert.rejects(pending, error);
await assert.rejects(thrown, error);
assert.deepStrictEqual(log, ['next', 'next', 'return']);
release();
await tick();
await tick();
assert.deepStrictEqual(log, ['next', 'next', 'return']);
}
// A stateful transform's generator waits for the source in user code: it
// still sees the read fail at once, and its finally block runs before the
// source's next() settles.
async function testPullStateful() {
await checkPendingRead((normalized, signal, log) => {
const transform = {
async *transform(source) {
try {
for await (const batch of source) yield batch;
} finally {
log.push('finally');
}
},
};
const it = pull(normalized, transform, { signal })[Symbol.asyncIterator]();
const late = [];
const readFirst = it.next();
const done = readFirst.then(() => it.next()).then((r) => late.push(r));
return { done, readFirst, extra: () => late };
}, { stateful: true });
}
async function testPipeTo(transforms) {
await checkPendingRead((normalized, signal) => {
const written = [];
let first;
const readFirst = new Promise((resolve) => { first = resolve; });
const writer = {
write(chunk) { written.push(chunk[0]); first(); },
end() {},
fail() {},
};
const done = pipeTo(normalized, ...transforms, writer, { signal });
return { done, readFirst, extra: () => written.slice(1) };
});
}
async function testBytes() {
await checkPendingRead((normalized, signal) => {
// bytes() gives nothing back before the end: the first read is done
// once the source has been read twice.
const done = bytes(normalized, { signal });
return { done, readFirst: tick(), extra: () => [] };
});
}
(async () => {
await testPull([]);
await testPull([(c) => c]);
await testPullReturn();
await testPullThrow();
await testPullStateful();
await testPipeTo([]);
await testPipeTo([(c) => c]);
await testBytes();
})().then(common.mustCall());