Skip to content
Myr

kill.js

This file

Path

lib/kill.js

109 lines · 2.8 KiB · JavaScript

Last changed by

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

History of this file

Take it

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 1–19why, one step back
1'use strict';
2const os = require('os');3const onExit = require('signal-exit');45const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;67// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior8const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {9 const killResult = kill(signal);10 setKillTimeout(kill, signal, options, killResult);11 return killResult;12};1314const setKillTimeout = (kill, signal, options, killResult) => {15 if (!shouldForceKill(signal, options, killResult)) {16 return;17 }1819 const timeout = getForceKillAfterTimeout(options);

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

line 20why, one step back
20 const t = setTimeout(() => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 21why, one step back
21 kill('SIGKILL');

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

lines 22–27why, one step back
22 }, timeout);
2324 // istanbul ignore else25 if (t.unref) {26 t.unref();27 }

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 28–59why, one step back
28};
2930const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {31 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;32};3334const isSigterm = signal => {35 return signal === os.constants.signals.SIGTERM ||36 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');37};3839const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {40 if (forceKillAfterTimeout === true) {41 return DEFAULT_FORCE_KILL_TIMEOUT;42 }4344 if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {45 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);46 }4748 return forceKillAfterTimeout;49};5051// `childProcess.cancel()`52const spawnedCancel = (spawned, context) => {53 const killResult = spawned.kill();5455 if (killResult) {56 context.isCanceled = true;57 }58};59

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 60–64why, one step back
60const timeoutKill = (spawned, signal, reject) => {
61 spawned.kill(signal);62 reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));63};64

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 65why, one step back
65// `timeout` option handling

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 66–68why, one step back
66const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {
67 if (timeout === 0 || timeout === undefined) {68 return spawnedPromise;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 69why, one step back
69 }

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 70–81why, one step back
70
71 if (!Number.isInteger(timeout) || timeout < 0) {72 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);73 }7475 let timeoutId;76 const timeoutPromise = new Promise((resolve, reject) => {77 timeoutId = setTimeout(() => {78 timeoutKill(spawned, killSignal, reject);79 }, timeout);80 });81

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

line 82why, one step back
82 const safeSpawnedPromise = spawnedPromise.finally(() => {

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 83–86why, one step back
83 clearTimeout(timeoutId);
84 });8586 return Promise.race([timeoutPromise, safeSpawnedPromise]);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 87–89why, one step back
87};
8889// `cleanup` option handling

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

line 90why, one step back
90const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 91why, one step back
91 if (!cleanup || detached) {

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

line 92why, one step back
92 return timedPromise;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 93–94why, one step back
93 }
94

Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047

line 95why, one step back
95 const removeExitHandler = onExit(() => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 96–98why, one step back
96 spawned.kill();
97 });98

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

lines 99–101why, one step back
99 return timedPromise.finally(() => {
100 removeExitHandler();101 });

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 102–107why, one step back
102};
103104module.exports = {105 spawnedKill,106 spawnedCancel,107 setupTimeout,

Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047

line 108why, one step back
108 setExitHandler

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 109why, one step back
109};