diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 5cc71c47c90..fde0944d318 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -674,7 +674,9 @@ added: * `writer` {Object} Destination with `write(chunk)` method. * `options` {Object} * `signal` {AbortSignal} Abort the pipeline. Aborting fails the destination - writer unless `preventFail` is `true`. + writer unless `preventFail` is `true`. The signal is passed to the + writer's `write()`, `writev()` and `end()` in an options object, the same + object for every call. * `preventClose` {boolean} If `true`, do not call `writer.end()` when the source ends. **Default:** `false`. * `preventFail` {boolean} If `true`, do not call `writer.fail()` on diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index ae68d2b3c13..c80068a7a25 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -73,7 +73,6 @@ const { toUint8Array, validateBatchEntry, validateByteView, - yieldAbortable, } = require('internal/streams/iter/utils'); const { converters, @@ -1737,6 +1736,11 @@ async function pipeTo(source, ...args) { const hasWriteSync = typeof writer.writeSync === 'function'; const normalized = from(source); + // The options passed to every write(), writev() and end(). + const writeOptions = signal ? { __proto__: null, signal } : undefined; + // Without transforms, whether `signal` has aborted the pipe: the batches + // are then no longer read or written (see raceSignal()). + let signalAborted = false; let totalBytes = 0; const hasWritev = typeof writer.writev === 'function'; @@ -1757,9 +1761,7 @@ async function pipeTo(source, ...args) { } validateByteView(view); } - const result = writer.write( - validateByteView(view), - signal ? { __proto__: null, signal } : undefined); + const result = writer.write(validateByteView(view), writeOptions); if (result !== undefined) { await result; } @@ -1792,10 +1794,11 @@ async function pipeTo(source, ...args) { // ignoring errors closing it. async function pipeSyncSource(iterator) { for (;;) { + if (signalAborted) return; let batch = iterator[kNextSyncBatch](); if (batch === undefined) { const result = await iterator.next(); - if (result.done) return; + if (result.done || signalAborted) return; batch = result.value; } else if (batch === null) { return; @@ -1804,16 +1807,87 @@ async function pipeTo(source, ...args) { const p = writeBatch(batch); if (p) await p; } catch (error) { - try { - await iterator.return(); - } catch { - // The error writing the batch is thrown. + if (!signalAborted) { + try { + await iterator.return(); + } catch { + // The error writing the batch is thrown. + } } throw error; } } } + // Write the batches of a source read with next(), as the for await...of + // loop below does, stopping once `signal` aborts. + async function pipeSourceWithSignal(iterator) { + for (;;) { + const result = await iterator.next(); + if (result.done || signalAborted) return; + try { + const p = writeBatch(result.value); + if (p) await p; + } catch (error) { + if (!signalAborted) { + try { + await iterator.return(); + } catch { + // The error writing the batch is thrown. + } + } + throw error; + } + } + } + + // Wait for `pipe(iterator)` to write the batches of `iterator`, the + // normalized source, unless `signal` aborts first. On an abort, the source + // is closed, without waiting, and the abort reason is thrown at once, as a + // pull() pipeline with `signal` would reject its pending pull; `piped` + // then stops before reading or writing another batch. This takes one + // listener and one promise for the whole pipe, instead of a layer reading + // the source for every batch. The listener is added first, as the signal + // can abort while the source is read. + function raceSignal(iterator, pipe) { + const { promise, resolve, reject } = PromiseWithResolvers(); + let settled = false; + function closeAndReject() { + try { + const returnMethod = iterator.return; + if (typeof returnMethod === 'function') { + markPromiseAsHandled(PromiseResolve( + FunctionPrototypeCall(returnMethod, iterator))); + } + } catch { + // The abort takes precedence over errors closing the source. + } + reject(signal.reason); + } + + function onAbort() { + if (settled) return; + settled = true; + signalAborted = true; + // The signal can abort while the source is called: close it once that + // call has returned. + PromisePrototypeThen(kResolvedPromise, closeAndReject); + } + signal.addEventListener('abort', onAbort, kNullOnceOption); + PromisePrototypeThen(pipe(iterator), (value) => { + if (settled) return; + settled = true; + signal.removeEventListener('abort', onAbort); + resolve(value); + }, (error) => { + if (settled) return; + settled = true; + signal.removeEventListener('abort', onAbort); + reject(error); + }); + return promise; + } + // Write the batches of an async source as the for await...of loop below // does. Nothing cancels the normalization while a batch is read: return() // is only called after an error writing one. @@ -1856,8 +1930,8 @@ async function pipeTo(source, ...args) { if (!hasWritevSync || !writer.writevSync(validateBatchEntry(entry))) { validateBatchEntry(entry); - const opts = signal ? { __proto__: null, signal } : undefined; - const writevResult = writer.writev(validateBatchEntry(entry), opts); + const writevResult = writer.writev( + validateBatchEntry(entry), writeOptions); if (writevResult === undefined) { validateBatchEntry(entry); totalBytes += entry.byteLength; @@ -1889,11 +1963,12 @@ async function pipeTo(source, ...args) { if (transforms.length === 0) { // Fast path: no transforms - iterate normalized source directly if (signal) { - for await (const batch of yieldAbortable(normalized, signal)) { - signal.throwIfAborted(); - const p = writeBatch(batch); - if (p) await p; - } + // 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. + const iterator = normalized[SymbolAsyncIterator](); + await raceSignal(iterator, iterator[kNextSyncBatch] !== undefined ? + pipeSyncSource : pipeSourceWithSignal); } else if (normalized[kNextSyncBatch] !== undefined) { await pipeSyncSource(normalized); } else if (normalized[kNextUncancellable] !== undefined) { @@ -1909,8 +1984,9 @@ async function pipeTo(source, ...args) { normalized, transforms, signal, signal !== undefined); if (signal) { + // The pipeline rejects its pending pull when `signal` aborts. for await (const batch of pipeline) { - signal.throwIfAborted(); + if (signal.aborted) throw signal.reason; const p = writeBatch(batch); if (p) await p; } @@ -1924,7 +2000,7 @@ async function pipeTo(source, ...args) { if (!options.preventClose) { if (!hasEndSync || writer.endSync() < 0) { - await writer.end?.(signal ? { __proto__: null, signal } : undefined); + await writer.end?.(writeOptions); } } } catch (error) { diff --git a/test/parallel/test-stream-iter-pipeto-signal.js b/test/parallel/test-stream-iter-pipeto-signal.js index 48385710d8e..57fa9fed530 100644 --- a/test/parallel/test-stream-iter-pipeto-signal.js +++ b/test/parallel/test-stream-iter-pipeto-signal.js @@ -195,6 +195,84 @@ async function testSignalAbortedWhileReadingSource() { } } +// write(), writev() and end() are all passed the same options object. +async function testWriteOptionsShared() { + const ac = new AbortController(); + const seen = []; + const writer = { + async write(chunk, options) { seen.push(options); }, + async writev(chunks, options) { seen.push(options); }, + async end(options) { seen.push(options); }, + }; + async function* source() { + yield [new Uint8Array([1])]; + yield [new Uint8Array([2]), new Uint8Array([3])]; + yield [new Uint8Array([4])]; + } + await pipeTo(source(), writer, { signal: ac.signal }); + assert.strictEqual(seen.length, 4); + assert.strictEqual(new Set(seen).size, 1); + assert.strictEqual(seen[0].signal, ac.signal); +} + +// The signal aborting while a chunk is written synchronously stops the pipe: +// no other chunk is written, the source is closed, and the pipe rejects with +// the abort reason. +async function testAbortDuringWriteSync() { + for (const sync of [true, false]) { + const ac = new AbortController(); + const reason = new Error('abort in writeSync'); + const written = []; + let closed = false; + function* syncSource() { + try { + for (let i = 0; i < 5; i++) yield [new Uint8Array([i])]; + } finally { + closed = true; + } + } + + async function* asyncSource() { + try { + for (let i = 0; i < 5; i++) yield [new Uint8Array([i])]; + } finally { + closed = true; + } + } + const writer = { + write: common.mustNotCall(), + writeSync(chunk) { + written.push(chunk[0]); + if (chunk[0] === 1) ac.abort(reason); + return true; + }, + fail: common.mustCall((error) => assert.strictEqual(error, reason)), + }; + await assert.rejects( + pipeTo(sync ? syncSource() : asyncSource(), writer, + { signal: ac.signal }), + reason); + assert.deepStrictEqual(written, [0, 1]); + await setTimeout(1); + assert.strictEqual(closed, true); + } +} + +// The signal aborting while a write is pending rejects the pipe at once, +// even if the write never completes. +async function testAbortWhileWritePending() { + const ac = new AbortController(); + const reason = new Error('abort while writing'); + const writer = { + write() { + ac.abort(reason); + return new Promise(() => {}); + }, + }; + await assert.rejects(pipeTo(from('a'), writer, { signal: ac.signal }), + reason); +} + async function testSignalListenersRemoved() { // No abort listener is left on the signal once reading completes, fails // or is aborted. @@ -226,4 +304,7 @@ Promise.all([ testPipeToLiveSignalWithTransformsCompletes(), testSignalAbortedWhileReadingSource(), testSignalListenersRemoved(), + testWriteOptionsShared(), + testAbortDuringWriteSync(), + testAbortWhileWritePending(), ]).then(common.mustCall());