Cancel generators when the process errors (#783)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 5aa90bd · Browse files · 3 files·+57 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+57 added−6 removed
- tests:+29 added−1 removed
- other:+28 added−5 removed
Provenance
- Commit
4b487ce- Tree
164da1c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5aa90bd
3 files changed+57 added−6 removed
modified:lib/stdio/transform.js+28 added-5 removed
2 unmodified lines345678891011111213141516171819204 unmodified lines25262728293031322829303334353637383940414243444525 unmodified lines717273747576777879808182838485868788899037 unmodified lines2 unmodified lines
// Transform an array of generator functions into a `Transform` stream.// `Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => { const state = {}; const transformMethod = transformAsync ? pushChunks.bind(undefined, transformChunk) ? pushChunks.bind(undefined, transformChunk, state) : pushChunksSync.bind(undefined, transformChunkSync); const finalMethod = transformAsync || finalAsync ? pushChunks.bind(undefined, finalChunks) ? pushChunks.bind(undefined, finalChunks, state) : pushChunksSync.bind(undefined, finalChunksSync); const destroyMethod = transformAsync || finalAsync ? destroyTransform.bind(undefined, state) : undefined;
return new Transform({ writableObjectMode, writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),4 unmodified lines }, flush(done) { finalMethod([generators], this, done); }, destroy: destroyMethod, });};
const pushChunks = callbackify(async (getChunks, args, transformStream) => { for await (const chunk of getChunks(...args)) { transformStream.push(chunk);const pushChunks = callbackify(async (getChunks, state, args, transformStream) => { state.currentIterable = getChunks(...args);
try { for await (const chunk of state.currentIterable) { transformStream.push(chunk); } } finally { delete state.currentIterable; }});
// For each new chunk, apply each `transform()` method25 unmodified lines yield * transformChunk(finalChunk, generators, index + 1); }};
// Cancel any ongoing async generator when the Transform is destroyed, e.g. when the process errorsconst destroyTransform = callbackify(async ({currentIterable}, error) => { if (currentIterable !== undefined) { await (error ? currentIterable.throw(error) : currentIterable.return()); return; }
if (error) { throw error; }});
// Duplicate the code above but as synchronous functions.// This is a performance optimization when the `transform`/`flush` function is synchronous, which is the common case.const pushChunksSync = (getChunksSync, args, transformStream, done) => { try {37 unmodified linesmodified:test/stdio/generator.js+22 added-1 removed
1 import {Buffer} from 'node:buffer';2+import {once} from 'node:events';3 import {readFile, writeFile, rm} from 'node:fs/promises';4 import {getDefaultHighWaterMark, PassThrough} from 'node:stream';5 import {setTimeout, scheduler} from 'node:timers/promises';6 import test from 'ava';697 unmodified lines704 childProcess.stdin.write('foobar\n');705 await t.throwsAsync(childProcess, {message: GENERATOR_ERROR_REGEXP});706 });707 708+const testGeneratorCancel = async (t, error) => {709+ const childProcess = execa('noop.js', {stdout: infiniteGenerator});710+ await once(childProcess.stdout, 'data');711+ childProcess.stdout.destroy(error);712+ await t.throwsAsync(childProcess);713+};714+715+test('Running generators are canceled on process abort', testGeneratorCancel, undefined);716+test('Running generators are canceled on process error', testGeneratorCancel, new Error('test'));717+718+const testGeneratorDestroy = async (t, transform) => {719+ const childProcess = execa('forever.js', {stdout: transform});720+ const error = new Error('test');721+ childProcess.stdout.destroy(error);722+ t.is(await t.throwsAsync(childProcess), error);723+};724+725+test('Generators are destroyed on process error, sync', testGeneratorDestroy, noopGenerator(false));726+test('Generators are destroyed on process error, async', testGeneratorDestroy, infiniteGenerator);727+707-test('Generators are canceled on early process exit', async t => {728+test('Generators are destroyed on early process exit', async t => {729 await t.throwsAsync(execa('noop.js', {stdout: infiniteGenerator, uid: -1}));730 });modified:test/stdio/iterable.js+7 added
119 unmodified lines120 };121 122 test('stdin option can be multiple iterables', testMultipleIterable, 0);123 test('stdio[*] option can be multiple iterables', testMultipleIterable, 3);124+125+test('stdin option iterable is canceled on process error', async t => {126+ const iterable = infiniteGenerator();127+ await t.throwsAsync(execa('stdin.js', {stdin: iterable, timeout: 1}), {message: /timed out/});128+ // eslint-disable-next-line no-unused-vars, no-empty129+ for await (const _ of iterable) {}130+});