Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor kill() again #280

Merged
merged 8 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const onExit = require('signal-exit');
const stdio = require('./lib/stdio');

const TEN_MEGABYTES = 1000 * 1000 * 10;
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;

const SPACES_REGEXP = / +/g;

Expand Down Expand Up @@ -224,18 +225,27 @@ function setKillTimeout(kill, signal, options, killResult) {
return;
}

const forceKillAfter = Number.isInteger(options.forceKillAfter) ?
options.forceKillAfter :
5000;
setTimeout(() => kill('SIGKILL'), forceKillAfter).unref();
const timeout = getForceKillAfterTimeout(options);
setTimeout(() => {
kill('SIGKILL');
}, timeout).unref();
}

function shouldForceKill(signal, options, killResult) {
return ((typeof signal === 'string' &&
signal.toUpperCase() === 'SIGTERM') ||
signal === os.constants.signals.SIGTERM) &&
options.forceKill !== false &&
killResult;
function shouldForceKill(signal, {forceKill}, killResult) {
return isSigterm(signal) && forceKill !== false && killResult;
}

function isSigterm(signal) {
return signal === os.constants.signals.SIGTERM ||
(typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

function getForceKillAfterTimeout({forceKillAfter = DEFAULT_FORCE_KILL_TIMEOUT}) {
if (!Number.isInteger(forceKillAfter) || forceKillAfter < 0) {
throw new TypeError(`Expected the \`forceKillAfter\` option to be a non-negative integer, got \`${forceKillAfter}\` (${typeof forceKillAfter})`);
}

return forceKillAfter;
}

const execa = (file, args, options) => {
Expand Down
16 changes: 14 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ if (process.platform !== 'win32') {
const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});

test('.kill() `forceKillAfter` should not be a float', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfter: 0.5});
}, {instanceOf: TypeError, message: /non-negative integer/});
});

test('.kill() `forceKillAfter` should not be negative', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfter: -1});
}, {instanceOf: TypeError, message: /non-negative integer/});
});
}

test('stripFinalNewline: true', async t => {
Expand Down Expand Up @@ -289,8 +301,8 @@ test('execa() returns a promise with kill() and pid', t => {
});

test('child_process.spawn() errors are propagated', async t => {
const {exitCodeName} = await t.throwsAsync(execa('noop', {uid: -1}));
t.is(exitCodeName, process.platform === 'win32' ? 'ENOTSUP' : 'EINVAL');
const {failed} = await t.throwsAsync(execa('noop', {uid: -1}));
t.true(failed);
});

test('child_process.spawnSync() errors are propagated with a correct shape', t => {
Expand Down