Fix timeout option not leaving early when on `sleep` mode (#199)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #157
authored by Guillaume Martigny · committed by Sindre Sorhus · parent 27c22e5 · Browse files · 3 files·+91 added−43 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+91 added−43 removed
- other:+91 added−43 removed
Provenance
- Commit
136b340- Tree
2722ff6- Author
Guillaume Martigny <guillaume.martigny@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
27c22e5
3 files changed+91 added−43 removed
added:fixtures/sleeper+4 added
1+#!/bin/bash2+3+sleep 54+echo "ok"modified:index.js+71 added-40 removed
265 unmodified lines266 spawned.kill(parsed.options.killSignal);267 }, parsed.options.timeout);268 }269 270+ const resolvable = (() => {271+ let extracted;272+ const promise = new Promise(resolve => {273+ extracted = resolve;274+ });275+ promise.resolve = extracted;276+ return promise;277+ })();278+279 const processDone = new Promise(resolve => {280 spawned.on('exit', (code, signal) => {281 cleanup();282+283+ if (timedOut) {284+ resolvable.resolve([285+ {code, signal}, '', '', ''286+ ]);287+ }288+289 resolve({code, signal});290 });291 292 spawned.on('error', error => {22 unmodified lines315 spawned.all.destroy();316 }317 }318 303- // TODO: Use native "finally" syntax when targeting Node.js 10304- const handlePromise = () => pFinally(Promise.all([305- processDone,306- getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),307- getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),308- getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})309- ]).then(results => { // eslint-disable-line promise/prefer-await-to-then310- const result = results[0];311- result.stdout = results[1];312- result.stderr = results[2];313- result.all = results[3];319+ const handlePromise = () => {320+ let processComplete = Promise.all([321+ processDone,322+ getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),323+ getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),324+ getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})325+ ]);326 315- if (result.error || result.code !== 0 || result.signal !== null || isCanceled) {316- const error = makeError(result, {317- joinedCommand,318- parsed,319- timedOut,320- isCanceled321- });327+ if (timeoutId) {328+ processComplete = Promise.race([329+ processComplete,330+ resolvable331+ ]);332+ }333 323- // TODO: missing some timeout logic for killed324- // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203325- // error.killed = spawned.killed || killed;326- error.killed = error.killed || spawned.killed;334+ const finalize = async () => {335+ const results = await processComplete;336 328- if (!parsed.options.reject) {329- return error;330- }337+ const result = results[0];338+ result.stdout = results[1];339+ result.stderr = results[2];340+ result.all = results[3];341 332- throw error;333- }342+ if (result.error || result.code !== 0 || result.signal !== null || isCanceled) {343+ const error = makeError(result, {344+ joinedCommand,345+ parsed,346+ timedOut,347+ isCanceled348+ });349 350+ // TODO: missing some timeout logic for killed351+ // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203352+ // error.killed = spawned.killed || killed;353+ error.killed = error.killed || spawned.killed;354+355+ if (!parsed.options.reject) {356+ return error;357+ }358+359+ throw error;360+ }361+335- return {336- stdout: handleOutput(parsed.options, result.stdout),337- stderr: handleOutput(parsed.options, result.stderr),338- all: handleOutput(parsed.options, result.all),339- code: 0,340- exitCode: 0,341- exitCodeName: 'SUCCESS',342- failed: false,343- killed: false,344- command: joinedCommand,345- timedOut: false,346- isCanceled: false362+ return {363+ stdout: handleOutput(parsed.options, result.stdout),364+ stderr: handleOutput(parsed.options, result.stderr),365+ all: handleOutput(parsed.options, result.all),366+ code: 0,367+ exitCode: 0,368+ exitCodeName: 'SUCCESS',369+ failed: false,370+ killed: false,371+ command: joinedCommand,372+ timedOut: false,373+ isCanceled: false374+ };375 };348- }), destroy);376 377+ // TODO: Use native "finally" syntax when targeting Node.js 10378+ return pFinally(finalize(), destroy);379+ };380+381 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);382 383 handleInput(spawned, parsed.options.input);384 76 unmodified linesmodified:test.js+16 added-3 removed
383 unmodified lines384 test('error.code is 2', code, 2);385 test('error.code is 3', code, 3);386 test('error.code is 4', code, 4);387 388-test('timeout will kill the process early', async t => {389- const error = await t.throwsAsync(execa('delay', ['60000', '0'], {timeout: 1500, message: TIMEOUT_REGEXP}));388+test.serial('timeout will kill the process early', async t => {389+ const time = Date.now();390+ const error = await t.throwsAsync(execa('delay', ['60000', '0'], {timeout: 500, message: TIMEOUT_REGEXP}));391+ const diff = Date.now() - time;392 393 t.true(error.timedOut);394 t.not(error.exitCode, 22);395+ t.true(diff < 4000);396 });397 398+test.serial('timeout will kill the process early (sleep)', async t => {399+ const time = Date.now();400+ const error = await t.throwsAsync(execa('sleeper', [], {timeout: 500, message: TIMEOUT_REGEXP}));401+ const diff = Date.now() - time;402+403+ t.true(error.timedOut);404+ t.not(error.stdout, 'ok');405+ t.true(diff < 4000);406+});407+408 test('timeout will not kill the process early', async t => {396- const error = await t.throwsAsync(execa('delay', ['3000', '22'], {timeout: 30000}), {code: 22, message: getExitRegExp('22')});409+ const error = await t.throwsAsync(execa('delay', ['2000', '22'], {timeout: 30000}), {code: 22, message: getExitRegExp('22')});410 t.false(error.timedOut);411 });412 413 test('timedOut will be false if no timeout was set and zero exit code', async t => {242 unmodified lines