diff --git a/lib/internal/streams/iter/consumers.js b/lib/internal/streams/iter/consumers.js index 6f967dddd1d..d195afea6b0 100644 --- a/lib/internal/streams/iter/consumers.js +++ b/lib/internal/streams/iter/consumers.js @@ -55,6 +55,7 @@ const { fromSync, isAsyncIterable, isSyncIterable, + kNextUncancellable, } = require('internal/streams/iter/from'); const { @@ -171,10 +172,15 @@ async function collectAsync(source, signal, limit) { // Read as for await...of does, stopping once the signal aborts; see // 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 }; async function consume(iterator) { + const uncancellable = iterator[kNextUncancellable] !== undefined; for (;;) { - const result = await iterator.next(); + const result = await (uncancellable ? + iterator[kNextUncancellable]() : iterator.next()); if (result.done || state.aborted) return; try { recordBatch(result.value); diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index 22821627ac8..269dc662d7b 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -152,8 +152,13 @@ function waitForNormalization(value, context) { const kNextSyncBatch = Symbol('kNextSyncBatch'); // 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 -// is pending, for pipeTo(): see createAsyncSourceNormalizer(). +// reads like next(), for internal callers that stop reading when they +// 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'); // Methods of the iterator returned by yieldNormalizationAbortable(), used by @@ -591,11 +596,12 @@ function yieldNormalizationAbortable(source, context) { }); return promise; }, - // Like next(), when nothing cancels the normalization while the read - // is pending, in two parts so that the caller handles the result in - // the same reaction: kReadUncancellable starts the read, returning a - // promise for the source's result, and the caller passes that result - // to kToIterResult, or calls kReadFailed if the promise rejects. + // Like next(), for a caller that ignores the read if it cancels the + // normalization while the read is pending (see kNextUncancellable), + // in two parts so that the caller handles the result in the same + // reaction: kReadUncancellable starts the read, returning a promise + // for the source's result, and the caller passes that result to + // kToIterResult, or calls kReadFailed if the promise rejects. [kReadUncancellable]() { if (completed) { return PromiseResolve(kDoneSourceResult); @@ -836,9 +842,11 @@ function createAsyncSourceNormalizer(source, context) { let boundedBatches = null; let valueBatches = null; // Whether the source is read with kNextUncancellable: set by the - // kNextUncancellable method, for callers that never cancel the - // normalization while a read is pending. + // kNextUncancellable method, for callers that ignore a read pending when + // they cancel the normalization. readReleased: whether a cancellation has + // released such a read (see onUncancellableCancelled()). let uncancellable = false; + let readReleased = false; // Whether the source is read with kRead, and the settling functions of // the read's promise while it is pending. let readable = false; @@ -915,6 +923,8 @@ function createAsyncSourceNormalizer(source, context) { // A kReadUncancellable result: handled as the result of the source's // next() is, in the same reaction. function onUncancellableResult(result) { + if (readReleased) throw context.reason; + context.resolve = null; let iterResult; try { iterResult = iterator[kToIterResult](result); @@ -925,10 +935,41 @@ function createAsyncSourceNormalizer(source, context) { } function onUncancellableError(error) { + if (readReleased) throw context.reason; + context.resolve = null; iterator[kReadFailed](); 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 // source's result, and settles the read's promise. function onRead(result) { @@ -954,10 +995,7 @@ function createAsyncSourceNormalizer(source, context) { } function pullSource() { - if (uncancellable) { - return PromisePrototypeThen(iterator[kReadUncancellable](), - onUncancellableResult, onUncancellableError); - } + if (uncancellable) return readUncancellable(); if (readable) { // One promise for the read, which a cancellation rejects, instead of // next()'s promise and a reaction to it. diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index 12a6b659b9e..109d51c8168 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -1233,20 +1233,44 @@ class PipelineSource { #abort; #iterator = null; #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.#abort = abort; + this.#uncancellable = uncancellable; } [SymbolAsyncIterator]() { return this; } + // Whether the source is read with kNextUncancellable (known once it has + // been read). + get uncancellable() { + return this.#uncancellable && this.#iterator !== null; + } + next() { if (this.#abort.aborted) return PromiseReject(this.#abort.reason); 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(); } @@ -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. * @param {AsyncIterable} normalized @@ -1377,6 +1408,8 @@ function createAsyncPipeline(source, transforms, signal, // runs one pull at a time. let resolvePull = null; let rejectPull = null; + // See stop(). + let closeWithoutWaiting = false; const operations = createOperationQueue(); const self = ObjectSetPrototypeOf({ @@ -1437,6 +1470,14 @@ function createAsyncPipeline(source, transforms, signal, if (rejectPull !== null) { abortPipeline(reason, false); 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 { abort.abort(reason); } @@ -1542,7 +1583,8 @@ function createAsyncPipeline(source, transforms, signal, if (state === kStart) { state = kActive; abort = new PipelineAbort(); - pipelineSource = new PipelineSource(source, abort); + pipelineSource = new PipelineSource( + source, abort, !hasTransformObject(transforms)); if (signal !== undefined) { signal.addEventListener('abort', onSignalAbort, { __proto__: null, once: true, @@ -1600,6 +1642,11 @@ function createAsyncPipeline(source, transforms, signal, } state = kDone; removeSignalListener(); + if (closeWithoutWaiting) { + markPromiseAsHandled(closeAsyncIterator(iterator, false)); + operations.settled(); + return PromiseResolve(result); + } return PromisePrototypeThen( closeAsyncIterator(iterator, false), () => { operations.settled(); @@ -1618,6 +1665,11 @@ function createAsyncPipeline(source, transforms, signal, } state = kDone; removeSignalListener(); + if (closeWithoutWaiting) { + markPromiseAsHandled(closeAsyncIterator(iterator, true)); + operations.settled(); + return PromiseReject(error); + } return PromisePrototypeThen( closeAsyncIterator(iterator, true), () => { 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 - // 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) { + const uncancellable = iterator[kNextUncancellable] !== undefined; for (;;) { - const result = await iterator.next(); + const result = await (uncancellable ? + iterator[kNextUncancellable]() : iterator.next()); if (result.done || abortState.aborted) return; try { const p = writeBatch(result.value); @@ -2144,9 +2200,8 @@ async function pipeTo(source, ...args) { if (transforms.length === 0) { // Fast path: no transforms - iterate normalized source directly if (signal) { - // Batches are read synchronously when possible, as without a signal. - // Async reads go through next(), which return() can cancel while it - // is pending, unlike kNextUncancellable. + // Batches are read synchronously when possible, as without a signal, + // otherwise as pipeSourceWithSignal() says. const iterator = normalized[SymbolAsyncIterator](); await raceSignal(iterator, signal, abortState, iterator[kNextSyncBatch] !== undefined ? diff --git a/test/parallel/test-stream-iter-abort-pending-read.js b/test/parallel/test-stream-iter-abort-pending-read.js new file mode 100644 index 00000000000..87a051a17df --- /dev/null +++ b/test/parallel/test-stream-iter-abort-pending-read.js @@ -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());