Fix `timeout` option (#727)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 1c48d37 · Browse files · 5 files·+11 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+11 added−7 removed
- tests:+4 added−4 removed
- other:+7 added−3 removed
Provenance
- Commit
a61a028- Tree
6d0e674- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
1c48d37
5 files changed+11 added−7 removed
modified:lib/error.js+1 added-1 removed
51 unmodified lines5253545556565758596048 unmodified lines51 unmodified lines command, escapedCommand, timedOut, isCanceled, options: {timeout, cwd = process.cwd()}, options: {timeoutDuration: timeout, cwd = process.cwd()},}) => { // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`. // We normalize them to `undefined` exitCode = exitCode === null ? undefined : exitCode;48 unmodified linesmodified:lib/stream.js+1 added-1 removed
129 unmodified lines130 131 // Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)132 export const getSpawnedResult = async ({133 spawned,134- options: {encoding, buffer, maxBuffer, timeout, killSignal, cleanup, detached},134+ options: {encoding, buffer, maxBuffer, timeoutDuration: timeout, killSignal, cleanup, detached},135 context,136 stdioStreamsGroups,137 controller,138 }) => {41 unmodified linesmodified:test/fixtures/no-killable.js+1 added-3 removed
7 unmodified lines891011121314127 unmodified lines
process.send('');console.log('.');
setInterval(() => { // Run forever}, 20_000);setTimeout(noop, 1e8);modified:test/kill.js+3 added-1 removed
83 unmodified lines84 t.is(signal, 'SIGKILL');85 t.true(isCanceled);86 });87 88+ // For this test to work, the process needs to spawn and setup its `SIGTERM` handler before `timeout` terminates it.89+ // This creates a race condition that we can only work around by using `test.serial()` and a higher timeout.90 test.serial('`forceKillAfterDelay` works with the "timeout" option', async t => {89- const {subprocess} = await spawnNoKillable(1, {timeout: 2e3});91+ const {subprocess} = await spawnNoKillable(1, {timeout: 5e3});92 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);93 t.true(isTerminated);94 t.is(signal, 'SIGKILL');95 t.true(timedOut);222 unmodified linesmodified:index.js+5 added-1 removed
101 unmodified lines102 killSignal,103 forceKillAfterDelay,104 });105 106+// Prevent passing the `timeout` option directly to `child_process.spawn()`107+const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});108+109 const handleOutput = (options, value) => {110 if (value === undefined || value === null) {111 return;112 }9 unmodified lines122 return options.stripFinalNewline ? stripFinalNewline(value) : value;123 };124 125 export function execa(rawFile, rawArgs, rawOptions) {123- const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);126+ const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);127+ const options = handleAsyncOptions(normalizedOptions);128 129 const stdioStreamsGroups = handleInputAsync(options);130 131 let spawned;217 unmodified lines