Skip to content
Myr

Destroy internal streams when process exits early (#770)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent db95b51 · Browse files · 8 files+45 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+45 added−8 removed

  • tests:+23 added−1 removed
  • other:+22 added−7 removed

8 files+45 added−8 removed

modified:lib/stdio/async.js+13 added-1 removed
modified:lib/stdio/native.js+1 added-3 removed
modified:lib/stdio/utils.js+4 added
modified:lib/stream.js+2 added-2 removed
2 unmodified lines
3import {setImmediate} from 'node:timers/promises';4import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';5import mergeStreams from '@sindresorhus/merge-streams';6import {throwOnTimeout, cleanupOnExit} from './kill.js';7import {STANDARD_STREAMS} from './stdio/native.js';7import {isStandardStream} from './stdio/utils.js';8import {generatorToDuplexStream} from './stdio/generator.js';910// `all` interleaves `stdout` and `stderr`11export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)
87 unmodified lines
99 .filter(({value, type, direction}) => !shouldWaitForCustomStream(value, type, direction))100 .map(({value}) => throwOnStreamError(value));101102const shouldWaitForCustomStream = (value, type, direction) => (type === 'fileUrl' || type === 'filePath')103 || (direction === 'output' && !STANDARD_STREAMS.includes(value));103 || (direction === 'output' && !isStandardStream(value));104105const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];106107const throwOnStreamError = async stream => {
75 unmodified lines
modified:test/helpers/generator.js+7 added-1 removed
1import {setImmediate} from 'node:timers/promises';1import {setImmediate, setInterval} from 'node:timers/promises';2import {foobarObject} from './input.js';34export const noopGenerator = (objectMode, binary) => ({5 * transform(line) {
52 unmodified lines
5859 const generatorOptions = typeof transform === 'function' ? {transform} : transform;60 return ({...generatorOptions, transform: noYieldTransform, final: generatorOptions.transform});61};6263export const infiniteGenerator = async function * () {64 for await (const value of setInterval(100, 'foo')) {65 yield value;66 }67};
modified:test/stdio/generator.js+5 added
15 unmodified lines
16 getOutputGenerator,17 outputObjectGenerator,18 noYieldGenerator,19 convertTransformToFinal,20 infiniteGenerator,21} from '../helpers/generator.js';2223setFixtureDir();24
677 unmodified lines
702 const childProcess = execa('stdin-fd.js', ['0'], {stdin: [noopGenerator(false), throwingGenerator, noopGenerator(false)]});703 childProcess.stdin.write('foobar\n');704 await t.throwsAsync(childProcess, {message: GENERATOR_ERROR_REGEXP});705});706707test('Generators are canceled on early process exit', async t => {708 await t.throwsAsync(execa('noop.js', {stdout: infiniteGenerator, uid: -1}));709});
modified:test/stdio/node-stream.js+11 added
129 unmodified lines
130 const {timedOut} = await t.throwsAsync(childProcess);131 t.true(timedOut);132 t.true(waitedForDestroy);133});134135const noopReadable = () => new Readable({read() {}});136const noopWritable = () => new Writable({write() {}});137138const testStreamEarlyExit = async (t, stream, streamName) => {139 await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1}));140 t.true(stream.destroyed);141};142143test('Input streams are canceled on early process exit', testStreamEarlyExit, noopReadable(), 'stdin');144test('Output streams are canceled on early process exit', testStreamEarlyExit, noopWritable(), 'stdout');
modified:index.js+2 added-1 removed
6 unmodified lines
7import crossSpawn from 'cross-spawn';8import stripFinalNewline from 'strip-final-newline';9import {npmRunPathEnv} from 'npm-run-path';10import {makeError} from './lib/error.js';11import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13import {normalizeStdioNode} from './lib/stdio/normalize.js';14import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay} from './lib/kill.js';15import {pipeToProcess} from './lib/pipe.js';
118 unmodified lines
134 let spawned;135 try {136 spawned = childProcess.spawn(file, args, options);137 } catch (error) {138 cleanupStdioStreams(stdioStreamsGroups);139 // Ensure the returned error is always both a promise and a child process140 const dummySpawned = new childProcess.ChildProcess();141 const errorInstance = makeError({142 error,
217 unmodified lines