diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index 1cb0276fc87..ae68d2b3c13 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -17,6 +17,7 @@ const { PromisePrototypeThen, PromiseReject, PromiseResolve, + PromiseWithResolvers, Symbol, SymbolAsyncIterator, SymbolIterator, @@ -39,11 +40,7 @@ const { isPromise, isUint8Array, } = require('internal/util/types'); -const { - AbortController, - AbortSignal, - abortSignal, -} = require('internal/abort_controller'); +const { kWeakHandler } = require('internal/event_target'); const { arrayBufferViewToUint8Array, @@ -1037,73 +1034,72 @@ async function* applyValidatedStatefulAsyncTransform( } /** - * Create an async pipeline from source through transforms. - * @param {AsyncIterable} source - * @param {Array} transforms - * @param {AbortSignal} [signal] - * @returns {AsyncIterator} + * The source of a pipeline, as its transforms read it: passes reads to the + * source's iterator as they are, so that reading costs nothing more, and + * lets the pipeline close the source's iterator when it is aborted, without + * waiting for a pending read to complete. The source's return() is called + * at most once. */ -function createAsyncPipeline(source, transforms, signal) { - if (transforms.length === 0) { - return createAsyncPipelineWithoutTransforms(source, signal); +class PipelineSource { + #source; + #abort; + #iterator = null; + #closed = false; + + constructor(source, abort) { + this.#source = source; + this.#abort = abort; } - return createAsyncTransformPipeline(source, transforms, signal); -} -/** - * The pipeline without transforms: what an async generator doing - * `signal?.throwIfAborted(); yield* yieldAbortable(source, signal);` would - * do. With a signal, it is written out by hand to avoid an async generator - * layer for every batch: the signal is checked by the first next(), then - * every call is passed to the iterator of yieldAbortable(), which queues - * them and completes as the generator would. - * @param {AsyncIterable} source - * @param {AbortSignal} [signal] - * @returns {AsyncIterator} - */ -function createAsyncPipelineWithoutTransforms(source, signal) { - if (signal === undefined) return yieldFrom(source); - let state = kStart; - let iterator; - return ObjectSetPrototypeOf({ - next() { - if (state === kStart) { - try { - // Check for abort - signal.throwIfAborted(); - iterator = yieldAbortable(source, signal)[SymbolAsyncIterator](); - } catch (error) { - state = kDone; - return PromiseReject(error); - } - state = kActive; - } else if (state === kDone) { - return PromiseResolve(new IterResult(true, undefined)); - } - return iterator.next(); - }, - return(value) { - if (state !== kActive) { - state = kDone; - return PromiseResolve(new IterResult(true, value)); - } - return iterator.return(value); - }, - throw(error) { - if (state !== kActive) { - state = kDone; - return PromiseReject(error); - } - return iterator.throw(error); - }, - [SymbolAsyncIterator]() { - return this; - }, - }, null); -} + [SymbolAsyncIterator]() { + return this; + } -async function* yieldFrom(source) { - yield* source; + next() { + if (this.#abort.aborted) return PromiseReject(this.#abort.reason); + if (this.#closed) return PromiseResolve(new IterResult(true, undefined)); + this.#iterator ??= this.#source[SymbolAsyncIterator](); + return this.#iterator.next(); + } + + return(value) { + const iterator = this.#iterator; + if (this.#closed || iterator === null) { + this.#closed = true; + return PromiseResolve(new IterResult(true, value)); + } + this.#closed = true; + const returnMethod = iterator.return; + if (returnMethod === undefined || returnMethod === null) { + return PromiseResolve(new IterResult(true, value)); + } + return FunctionPrototypeCall(returnMethod, iterator, value); + } + + throw(error) { + // As for await closes the source when its body throws. + return PromisePrototypeThen(this.return(), () => { throw error; }); + } + + // Close the source's iterator for an abort, without waiting, ignoring + // errors from closing it. + close() { + const iterator = this.#iterator; + if (this.#closed || iterator === null) { + this.#closed = true; + return; + } + this.#closed = true; + try { + const returnMethod = iterator.return; + if (returnMethod !== undefined && returnMethod !== null) { + markPromiseAsHandled(PromiseResolve( + FunctionPrototypeCall(returnMethod, iterator))); + } + } catch { + // The abort takes precedence over errors closing the source. + } + } } /** @@ -1154,163 +1150,280 @@ function createAsyncTransformLayers(normalized, transforms, abort) { } /** - * The pipeline through one or more transforms: an async iterator doing what - * an async generator would, written out by hand to avoid an async generator - * layer for every batch (see applyFusedStatelessAsyncTransforms()). + * The iterator of a pull() pipeline, also used by pipeTo() for transforms: an + * async iterator doing what an async generator would, written out by hand to + * avoid an async generator layer for every batch (see + * applyFusedStatelessAsyncTransforms()). * - * When started by the first next(), it checks `signal`, then creates the - * abort state whose signal the transforms get, aborted when `signal` aborts. - * Each batch, and the end, is passed on only if the pipeline has not been - * aborted. If the pipeline fails, the transforms' signal is aborted - * with the error; if it is stopped early by return() or throw(), the - * transforms are closed and their signal is aborted. + * The pipeline's abort state (a PipelineAbort) gives the transforms their + * signal. The pipeline is aborted when `signal` aborts, when the consumer + * stops early with return() or throw(), and with the error when it fails. + * An abort is handled here, once, instead of in every layer: + * - a pull that is pending rejects at once with the abort reason, after the + * source's return() is called, without waiting for it (any later result of + * the transforms is ignored); + * - the source is read through a PipelineSource, which rejects reads once + * the pipeline is aborted; + * - each batch, and the end, is passed on only if the pipeline has not been + * aborted. + * When `signal` aborts, the pull that observes it and every later pull reject + * with its reason, and the transforms are closed. When the consumer stops, + * later pulls report the end. + * + * A pending pull can only be rejected at once if it is a promise of its own, + * one more for every batch. Without a signal, pipeTo() never stops the + * pipeline while a pull is pending, so it passes `interruptible` as false + * and each pull is the promise of the reaction to the transforms' result. * @param {AsyncIterable} source * @param {Array} transforms * @param {AbortSignal} [signal] + * @param {boolean} [interruptible] * @returns {AsyncIterator} */ -function createAsyncTransformPipeline(source, transforms, signal) { +function createAsyncPipeline(source, transforms, signal, + interruptible = true) { let state = kStart; + // Whether `signal` has aborted the pipeline: pulls then reject. + let signalAborted = signal !== undefined && signal.aborted; let abort; - let abortHandler; - let completed = false; + let pipelineSource; let iterator; let nextMethod; + // The settling functions of the pending pull, if any. The operation queue + // runs one pull at a time. + let resolvePull = null; + let rejectPull = null; const operations = createOperationQueue(); - // What an async generator would do in its `finally` block. - function cleanup() { - if (!completed && !abort.aborted) { - // Consumer stopped early or return() was called. - // If a transform listener throws here, let it propagate. - abort.abort(lazyDOMException('Aborted', 'AbortError')); - } - // Clean up user signal listener to prevent holding controller alive - if (signal && abortHandler) { - signal.removeEventListener('abort', abortHandler); + const self = ObjectSetPrototypeOf({ + next() { return operations.run(doNext); }, + return(value) { + stop(lazyDOMException('Aborted', 'AbortError')); + return operations.run(doReturn, value); + }, + throw(error) { + stop(error); + return operations.run(doThrow, error); + }, + [SymbolAsyncIterator]() { return this; }, + }, null); + + function onSignalAbort() { + if (state === kDone) return; + signalAborted = true; + // Close the transforms too; their errors are ignored, the pull rejects + // with the abort reason. + abortPipeline(signal.reason, true); + } + + function removeSignalListener() { + if (signal !== undefined && state !== kStart) { + signal.removeEventListener('abort', onSignalAbort); } } - function finish(result) { - operations.settled(); - return result; - } - - function complete(result) { + // Abort the pipeline: the transforms' signal at once, then close the + // source and reject the pending pull, if any. The signal can abort while + // the source or a transform is called, so these wait for a microtask, for + // that call to return. A result of the transforms that comes later is + // ignored. + function abortPipeline(reason, closeTransforms) { state = kDone; - try { - cleanup(); - } finally { - operations.settled(); - } - return result; + removeSignalListener(); + abort.abort(reason); + const reject = rejectPull; + resolvePull = rejectPull = null; + PromisePrototypeThen(kResolvedPromise, () => { + pipelineSource.close(); + if (reject !== null) { + operations.settled(); + reject(reason); + } + if (closeTransforms) { + markPromiseAsHandled(closeAsyncIterator(iterator, true)); + } + }); } - // What an async generator would do in its `catch` block, then `finally`. - function fail(error) { + // The consumer stops early: abort the pipeline at once, so that the + // transforms see their signal aborted before they are closed and a pending + // pull rejects; doReturn() or doThrow() then closes the transforms. + function stop(reason) { + if (state !== kActive) return; + if (rejectPull !== null) { + abortPipeline(reason, false); + state = kActive; + } else { + abort.abort(reason); + } + } + + // The end of the pipeline, for an error: what an async generator would do + // in its `catch` block, then `finally`. + function failed(error) { state = kDone; - try { - try { - abort.abort(error); - } finally { - cleanup(); - } - } finally { - operations.settled(); - } - throw error; + removeSignalListener(); + abort.abort(error); } - function onResult(result) { - let value; - try { - if ((typeof result !== 'object' && typeof result !== 'function') || - result === null) { - throw new ERR_INVALID_RETURN_VALUE( - 'an object', 'iterator.next()', result); - } - if (result.done) { - // A transform can abort while completing without producing a final - // batch, for example when an async flush resolves to null. In that - // case there is no batch with which to observe the abort. - abort.throwIfAborted(); - completed = true; - return complete(new IterResult(true, undefined)); - } - value = result.value; - } catch (error) { - return fail(error); + // What a pull settles with, given the result of the transforms: a result, + // or a promise for one, or it throws. The pipeline is done if it throws or + // the result is done. + function handleResult(result) { + if ((typeof result !== 'object' && typeof result !== 'function') || + result === null) { + throw new ERR_INVALID_RETURN_VALUE( + 'an object', 'iterator.next()', result); + } + if (result.done) { + // A transform can abort while completing without producing a final + // batch, for example when an async flush resolves to null. In that + // case there is no batch with which to observe the abort. + abort.throwIfAborted(); + state = kDone; + removeSignalListener(); + return new IterResult(true, undefined); } if (abort.aborted) { const error = abort.reason; // Like for await when its body throws: close the transforms first. return PromisePrototypeThen( - closeAsyncIterator(iterator, true), () => fail(error)); + closeAsyncIterator(iterator, true), () => { throw error; }); } - return finish(new IterResult(false, value)); + return new IterResult(false, result.value); } - function pullTransforms() { - let promise; + function onResult(result) { + const resolve = resolvePull; + if (resolve === null) return; // Settled by an abort. + const reject = rejectPull; + let settled; try { - promise = PromiseResolve(FunctionPrototypeCall(nextMethod, iterator)); + settled = handleResult(result); } catch (error) { - return fail(error); + onError(error); + return; } - return PromisePrototypeThen(promise, onResult, fail); + if (isPromise(settled)) { + PromisePrototypeThen(settled, undefined, (error) => { + if (rejectPull === reject) onError(error); + }); + return; + } + resolvePull = rejectPull = null; + operations.settled(); + resolve(settled); + } + + function onError(error) { + const reject = rejectPull; + if (reject === null) return; // Settled by an abort. + resolvePull = rejectPull = null; + operations.settled(); + failed(error); + reject(error); + } + + // onResult() and onError() for a pull that is the promise of the reaction + // to the transforms' result (see `interruptible`). + function onDirectResult(result) { + let settled; + try { + settled = handleResult(result); + } catch (error) { + return onDirectError(error); + } + if (isPromise(settled)) { + return PromisePrototypeThen(settled, undefined, onDirectError); + } + operations.settled(); + return settled; + } + + function onDirectError(error) { + operations.settled(); + failed(error); + throw error; } function doNext() { + if (signalAborted) { + operations.settled(); + return PromiseReject(signal.reason); + } if (state === kDone) { - return PromiseResolve(finish(new IterResult(true, undefined))); + operations.settled(); + return PromiseResolve(new IterResult(true, undefined)); } if (state === kStart) { - try { - // Check for abort - signal?.throwIfAborted(); - } catch (error) { - state = kDone; - operations.settled(); - return PromiseReject(error); - } state = kActive; - const normalized = yieldAbortable(source, signal); abort = new PipelineAbort(); - if (signal) { - abortHandler = () => { - abort.abort(signal.reason); - }; - signal.addEventListener('abort', abortHandler, kNullOnceOption); + pipelineSource = new PipelineSource(source, abort); + if (signal !== undefined) { + signal.addEventListener('abort', onSignalAbort, + { __proto__: null, once: true, + [kWeakHandler]: self }); } try { - const current = createAsyncTransformLayers( - normalized, transforms, abort); - iterator = current[SymbolAsyncIterator](); + if (transforms.length === 0) { + iterator = pipelineSource; + } else { + iterator = createAsyncTransformLayers( + pipelineSource, transforms, abort)[SymbolAsyncIterator](); + } nextMethod = iterator.next; } catch (error) { - try { - fail(error); - } catch (failure) { - return PromiseReject(failure); - } + state = kDone; + removeSignalListener(); + operations.settled(); + abort.abort(error); + return PromiseReject(error); } } - try { - return PromiseResolve(pullTransforms()); - } catch (error) { - return PromiseReject(error); + if (!interruptible) { + let next; + try { + next = PromiseResolve(FunctionPrototypeCall(nextMethod, iterator)); + } catch (error) { + operations.settled(); + failed(error); + return PromiseReject(error); + } + return PromisePrototypeThen(next, onDirectResult, onDirectError); } + // The pull is pending from now on: the signal can abort while the + // transforms or the source are called. + const { promise, resolve, reject } = PromiseWithResolvers(); + resolvePull = resolve; + rejectPull = reject; + let next; + try { + next = PromiseResolve(FunctionPrototypeCall(nextMethod, iterator)); + } catch (error) { + onError(error); + return promise; + } + PromisePrototypeThen(next, onResult, onError); + return promise; } function doReturn(value) { const result = new IterResult(true, value); if (state !== kActive) { state = kDone; - return PromiseResolve(finish(result)); + operations.settled(); + return PromiseResolve(result); } state = kDone; + removeSignalListener(); return PromisePrototypeThen( - closeAsyncIterator(iterator, false), () => complete(result), fail); + closeAsyncIterator(iterator, false), () => { + operations.settled(); + return result; + }, (error) => { + operations.settled(); + throw error; + }); } function doThrow(error) { @@ -1320,16 +1433,15 @@ function createAsyncTransformPipeline(source, transforms, signal) { return PromiseReject(error); } state = kDone; + removeSignalListener(); return PromisePrototypeThen( - closeAsyncIterator(iterator, true), () => fail(error)); + closeAsyncIterator(iterator, true), () => { + operations.settled(); + throw error; + }); } - return ObjectSetPrototypeOf({ - next() { return operations.run(doNext); }, - return(value) { return operations.run(doReturn, value); }, - throw(error) { return operations.run(doThrow, error); }, - [SymbolAsyncIterator]() { return this; }, - }, null); + return self; } // ============================================================================= @@ -1381,76 +1493,15 @@ function pull(source, ...args) { return { __proto__: null, [SymbolAsyncIterator]() { - if (signal === undefined) { - const controller = new AbortController(); - const iterator = createAsyncPipeline( - normalized, transforms, controller.signal); - return ObjectSetPrototypeOf({ - next(value) { - return iterator.next(value); - }, - return(value) { - controller.abort(lazyDOMException('Aborted', 'AbortError')); - return iterator.return(value); - }, - throw(error) { - abortSignal(controller.signal, error); - return iterator.throw(error); - }, - [SymbolAsyncIterator]() { - return this; - }, - }, null); + // Without transforms or a signal, the pipeline is the source. + if (transforms.length === 0 && signal === undefined) { + return normalized[SymbolAsyncIterator](); } - return createAbortablePullIterator(normalized, transforms, signal); + return createAsyncPipeline(normalized, transforms, signal); }, }; } -// Once `signal` aborts the pipeline, the pull that observed it rejects and -// so does every later pull, with the abort reason. That includes the case of -// an already-aborted signal, where the pipeline is never started. A plain -// async generator would instead complete after throwing, so later pulls -// would report a clean end of the stream. -function createAbortablePullIterator(source, transforms, signal) { - let aborted = signal.aborted; - let controller; - let iterator; - if (!aborted) { - controller = new AbortController(); - const iteratorSignal = AbortSignal.any([signal, controller.signal]); - iterator = createAsyncPipeline(source, transforms, iteratorSignal); - } - - function onRejected(error) { - if (signal.aborted) aborted = true; - throw error; - } - - return ObjectSetPrototypeOf({ - next(value) { - if (aborted) return PromiseReject(signal.reason); - return PromisePrototypeThen(iterator.next(value), undefined, onRejected); - }, - return(value) { - if (aborted) { - return PromiseResolve(new IterResult(true, value)); - } - controller.abort(lazyDOMException('Aborted', 'AbortError')); - return iterator.return(value); - }, - throw(error) { - if (aborted) return PromiseReject(error); - abortSignal(controller.signal, error); - return PromisePrototypeThen(iterator.throw(error), undefined, - onRejected); - }, - [SymbolAsyncIterator]() { - return this; - }, - }, null); -} - // Keep ownership of a bonded consumer outside the transform pipeline so it can // be detached even when the pipeline never starts or terminates early. function pullWithConsumerCleanup(source, transforms, signal) { @@ -1854,7 +1905,8 @@ async function pipeTo(source, ...args) { } } } else { - const pipeline = createAsyncPipeline(normalized, transforms, signal); + const pipeline = createAsyncPipeline( + normalized, transforms, signal, signal !== undefined); if (signal) { for await (const batch of pipeline) { diff --git a/test/parallel/test-stream-iter-pipeto-signal.js b/test/parallel/test-stream-iter-pipeto-signal.js index 3645db1a92f..48385710d8e 100644 --- a/test/parallel/test-stream-iter-pipeto-signal.js +++ b/test/parallel/test-stream-iter-pipeto-signal.js @@ -8,7 +8,7 @@ const common = require('../common'); const assert = require('assert'); const { setTimeout } = require('timers/promises'); const { getEventListeners } = require('events'); -const { bytes, pipeTo, from } = require('stream/iter'); +const { bytes, pipeTo, pull, from } = require('stream/iter'); async function testPipeToPreAbortedSignalFailsWriter() { const reason = new Error('already aborted'); @@ -165,9 +165,13 @@ async function testSignalAbortedWhileReadingSource() { // The signal can abort while the source is producing a value; the read // must still reject with the abort reason, and the source be closed, even // if the value never comes. + const identity = (chunks) => chunks; for (const consume of [ (source, signal) => pipeTo(source, { write() {} }, { signal }), + (source, signal) => pipeTo(source, identity, { write() {} }, { signal }), (source, signal) => bytes(source, { signal }), + (source, signal) => bytes(pull(source, { signal })), + (source, signal) => bytes(pull(source, identity, { signal })), ]) { const ac = new AbortController(); const reason = new Error('aborted while reading'); diff --git a/test/parallel/test-stream-iter-pull-async.js b/test/parallel/test-stream-iter-pull-async.js index 4c1bc0a7a14..c8293257712 100644 --- a/test/parallel/test-stream-iter-pull-async.js +++ b/test/parallel/test-stream-iter-pull-async.js @@ -18,6 +18,7 @@ const { const { setImmediate } = require('timers/promises'); const { inspect } = require('util'); +const { getEventListeners } = require('events'); async function testPullIdentity() { const data = await text(pull(from('hello-async'))); @@ -365,6 +366,68 @@ async function testPullSignalAbortWithTransformWhileSourceNextPending() { await assert.rejects(next, { name: 'AbortError' }); } +// An abort rejects a pending pull at once, wherever the pipeline is waiting: +// here, on a transform that never settles. +async function testPullSignalAbortWhileTransformPending() { + const ac = new AbortController(); + let transformSignal; + const iter = pull(from('a'), (chunks, options) => { + transformSignal = options.signal; + return new Promise(() => {}); + }, { signal: ac.signal })[Symbol.asyncIterator](); + const next = iter.next(); + await setImmediate(); + const reason = new Error('stop'); + ac.abort(reason); + await assert.rejects(next, reason); + assert.strictEqual(transformSignal.reason, reason); + await assert.rejects(iter.next(), reason); +} + +// When the signal aborts while no pull is pending, the source is closed once, +// and the next pull rejects. +async function testPullSignalAbortWhileIdleClosesSource() { + const log = []; + const ac = new AbortController(); + const iter = pull(createLoggedSource(log, [1, 2, 3]), (chunks) => chunks, + { signal: ac.signal })[Symbol.asyncIterator](); + assert.strictEqual((await iter.next()).done, false); + const reason = new Error('stop'); + ac.abort(reason); + await setImmediate(); + assert.deepStrictEqual(log, ['next 0', 'return']); + await assert.rejects(iter.next(), reason); + assert.strictEqual((await iter.return()).done, true); + assert.deepStrictEqual(log, ['next 0', 'return']); +} + +// The pipeline's listener on the signal is removed once the pipeline is +// done, however it ends. +async function testPullSignalListenerRemoved() { + const identity = (chunks) => chunks; + const ac = new AbortController(); + const { signal } = ac; + await text(pull(from(['a', 'b']), identity, { signal })); + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); + + let iter = pull(from(['a', 'b']), identity, { signal })[Symbol.asyncIterator](); + await iter.next(); + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); + await iter.return(); + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); + + iter = pull(from(['a', 'b']), () => { throw new Error('failed'); }, + { signal })[Symbol.asyncIterator](); + await assert.rejects(iter.next(), /failed/); + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); + + iter = pull(from(['a', 'b']), identity, { signal })[Symbol.asyncIterator](); + await iter.next(); + ac.abort(); + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); + await assert.rejects(iter.next(), { name: 'AbortError' }); +} + // Pull consumer break (return()) cleans up transform signal async function testPullConsumerBreakCleanup() { let signalAborted = false; @@ -744,8 +807,9 @@ async function testTransformSourceErrorDoesNotCloseSource() { } async function testPipeToTransformsStoppedEarly() { - // When the writer fails, the transforms are closed, closing the source, and - // their signal is aborted. + // When the writer fails, the transforms' signal is aborted, and the + // transforms are closed, closing the source, as when the consumer of pull() + // stops early. const log = []; let writes = 0; await assert.rejects(pipeTo( @@ -755,8 +819,8 @@ async function testPipeToTransformsStoppedEarly() { }, }), /write failed/); assert.deepStrictEqual(log, [ - 'next 0', 'transform 1', 'next 1', 'transform 2', 'return', - 'abort: Aborted', + 'next 0', 'transform 1', 'next 1', 'transform 2', 'abort: Aborted', + 'return', ]); } @@ -817,6 +881,9 @@ async function testTransformReturnClosesOutputAndSource() { testTransformOptionsNotShared(), testTransformOptionsShape(), testTransformSignalReadAfterAbort(), + testPullSignalAbortWhileTransformPending(), + testPullSignalAbortWhileIdleClosesSource(), + testPullSignalListenerRemoved(), testTransformOptionsSignalAssignable(), testTransformErrorClosesSource(), testTransformSourceErrorDoesNotCloseSource(),