Skip to content
Myr

Refactor `subprocess.kill(error)` (#1023)

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

authored by ehmicky · committed by GitHub · parent c5bd9d8 · Browse files · 8 files+26 added−30 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+26 added−30 removed

  • tests:+1 added−1 removed
  • other:+25 added−29 removed

8 files+26 added−30 removed

modified:lib/convert/concurrent.js+1 added-1 removed
modified:lib/convert/readable.js+1 added-1 removed
modified:lib/convert/shared.js-8 removed
modified:lib/methods/main-async.js+7 added-2 removed
15 unmodified lines
16import {logEarlyResult} from '../verbose/complete.js';17import {makeAllStream} from '../resolve/all-async.js';18import {waitForSubprocessResult} from '../resolve/wait-subprocess.js';19import {addConvertedStreams} from '../convert/add.js';20import {createDeferred} from '../utils/deferred.js';21import {mergePromise} from './promise.js';2223// Main shared logic for all async methods: `execa()`, `execaCommand()`, `$`, `execaNode()`24export const execaCoreAsync = (rawFile, rawArguments, rawOptions, createNested) => {
75 unmodified lines
100 const originalStreams = [...subprocess.stdio];101 pipeOutputAsync(subprocess, fileDescriptors, controller);102 cleanupOnExit(subprocess, options, controller);103104 const onInternalError = createDeferred();105 subprocess.kill = subprocessKill.bind(undefined, {106 kill: subprocess.kill.bind(subprocess),105 subprocess,107 options,108 onInternalError,109 controller,110 });111 subprocess.all = makeAllStream(subprocess, options);112 addConvertedStreams(subprocess, options);
6 unmodified lines
119 fileDescriptors,120 originalStreams,121 command,122 escapedCommand,123 onInternalError,124 controller,125 });126 return {subprocess, promise};127};128129// Asynchronous logic, as opposed to the previous logic which can be run synchronously, i.e. can be returned to user right away127const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller}) => {130const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, onInternalError, controller}) => {131 const context = {timedOut: false};132133 const [errorInfo, [exitCode, signal], stdioResults, allResult] = await waitForSubprocessResult({134 subprocess,
1 unmodified line
136 context,137 verboseInfo,138 fileDescriptors,139 originalStreams,140 onInternalError,141 controller,142 });143 controller.abort();144 onInternalError.resolve();145146 const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));147 const all = stripNewline(allResult, options, 'all');148 const result = getAsyncResult({
38 unmodified lines
modified:lib/resolve/wait-subprocess.js+2 added-9 removed
1import {once} from 'node:events';2import {isStream as isNodeStream} from 'is-stream';3import {errorSignal} from '../terminate/kill.js';3import {throwOnTimeout} from '../terminate/timeout.js';4import {isStandardStream} from '../utils/standard-stream.js';5import {TRANSFORM_TYPES} from '../stdio/type.js';6import {getBufferedData} from '../io/contents.js';
9 unmodified lines
16 context,17 verboseInfo,18 fileDescriptors,19 originalStreams,20 onInternalError,21 controller,22}) => {23 const exitPromise = waitForExit(subprocess);24 const streamInfo = {
36 unmodified lines
61 allPromise,62 ...originalPromises,63 ...customStreamsEndPromises,64 ]),65 onInternalError,66 throwOnSubprocessError(subprocess, controller),66 throwOnInternalError(subprocess, controller),67 ...throwOnTimeout(subprocess, timeout, context, controller),68 ]);69 } catch (error) {70 return Promise.all([
28 unmodified lines
99const throwOnSubprocessError = async (subprocess, {signal}) => {100 const [error] = await once(subprocess, 'error', {signal});101 throw error;102};103104// Fails right away when calling `subprocess.kill(error)`.105// Does not wait for actual signal termination.106const throwOnInternalError = async (subprocess, {signal}) => {107 const [error] = await once(subprocess, errorSignal, {signal});108 throw error;109};
modified:lib/terminate/kill.js+7 added-8 removed
21 unmodified lines
22const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;2324// Monkey-patches `subprocess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`25export const subprocessKill = (26 {kill, subprocess, options: {forceKillAfterDelay, killSignal}, controller},26 {kill, options: {forceKillAfterDelay, killSignal}, onInternalError, controller},27 signalOrError,28 errorArgument,29) => {30 const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);31 emitKillError(subprocess, error);31 emitKillError(error, onInternalError);32 const killResult = kill(signal);33 setKillTimeout({34 kill,35 signal,
20 unmodified lines
5657 return {signal, error};58};5960// Fails right away when calling `subprocess.kill(error)`.61// Does not wait for actual signal termination.62// Uses a deferred promise instead of the `error` event on the subprocess, as this is less intrusive.60const emitKillError = (subprocess, error) => {63const emitKillError = (error, onInternalError) => {64 if (error !== undefined) {62 subprocess.emit(errorSignal, error);65 onInternalError.reject(error);66 }67};6866// Like `error` signal but internal to Execa.67// E.g. does not make subprocess crash when no `error` listener is set.68export const errorSignal = Symbol('error');6969const setKillTimeout = async ({kill, signal, forceKillAfterDelay, killSignal, killResult, controller}) => {70 if (!shouldForceKill(signal, forceKillAfterDelay, killSignal, killResult)) {71 return;72 }
15 unmodified lines
added:lib/utils/deferred.js+7 added
1export const createDeferred = () => {2 const methods = {};3 const promise = new Promise((resolve, reject) => {4 Object.assign(methods, {resolve, reject});5 });6 return Object.assign(promise, methods);7};
modified:test/resolve/wait-subprocess.js+1 added-1 removed
17 unmodified lines
18test('stdio[*] is undefined if ignored - sync', testIgnore, 3, execaSync);1920const testSubprocessEventsCleanup = async (t, fixtureName) => {21 const subprocess = execa(fixtureName, {reject: false});22 t.deepEqual(subprocess.eventNames().map(String).sort(), ['Symbol(error)', 'error', 'exit', 'spawn']);22 t.deepEqual(subprocess.eventNames().map(String).sort(), ['error', 'exit', 'spawn']);23 await subprocess;24 t.deepEqual(subprocess.eventNames(), []);25};26
23 unmodified lines