Wait for streams completion on errors (#722)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 5eeaab5 · Browse files · 2 files·+20 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+20 added−1 removed
- tests:+17 added−0 removed
- other:+3 added−1 removed
Provenance
- Commit
195369f- Tree
8121c97- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5eeaab5
2 files changed+20 added−1 removed
modified:lib/stream.js+3 added-1 removed
116 unmodified linesExpand all1171181191201211221231241251261271281281291301311321335 unmodified lines1391401411421431441451461472 unmodified lines116 unmodified linesExpand all const customStreams = getCustomStreams(stdioStreamsGroups);
const stdioPromises = spawned.stdio.map((stream, index) => getStdioPromise({stream, stdioStreams: stdioStreamsGroups[index], encoding, buffer, maxBuffer})); const allPromise = getAllPromise({spawned, encoding, buffer, maxBuffer}); const customStreamsEndPromises = waitForCustomStreamsEnd(customStreams);
try { return await Promise.race([ Promise.all([ once(spawned, 'exit'), Promise.all(stdioPromises), allPromise, ...waitForCustomStreamsEnd(customStreams), ...customStreamsEndPromises, ]), ...throwOnCustomStreamsError(customStreams), ...throwIfStreamError(spawned.stdin), ...throwOnTimeout({spawned, timeout, killSignal, context, controller}),5 unmodified lines Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))), getBufferedData(allPromise, encoding), ]); cleanupStdioStreams(customStreams, error); await Promise.allSettled(customStreamsEndPromises); return results; } finally { controller.abort(); removeExitHandler?.();2 unmodified linesmodified:test/stdio/node-stream.js+17 added
1 import {once} from 'node:events';2 import {createReadStream, createWriteStream} from 'node:fs';3 import {readFile, writeFile, rm} from 'node:fs/promises';4 import {Readable, Writable, PassThrough} from 'node:stream';5+import {setTimeout} from 'node:timers/promises';6+import {callbackify} from 'node:util';7 import test from 'ava';8 import tempfile from 'tempfile';9 import {execa, execaSync} from '../../index.js';10 import {setFixtureDir} from '../helpers/fixtures-dir.js';104 unmodified lines115 116 test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);117 test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);118 test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);119+120+test('Wait for custom streams destroy on process errors', async t => {121+ let waitedForDestroy = false;122+ const stream = new Writable({123+ destroy: callbackify(async error => {124+ await setTimeout(0);125+ waitedForDestroy = true;126+ return error;127+ }),128+ });129+ const childProcess = execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1});130+ const {timedOut} = await t.throwsAsync(childProcess);131+ t.true(timedOut);132+ t.true(waitedForDestroy);133+});