kill.js
This file
Path
lib/kill.js
112 lines · 2.9 KiB · JavaScript
Last changed by
Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405
History of this fileTake it
Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 1–19why, one step back1'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 back20 const t = setTimeout(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 21why, one step back21 kill('SIGKILL');Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 22–23why, one step back22 }, timeout);23Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405
lines 24–26why, one step back24 // Guarded because there's no `.unref()` when `execa` is used in the renderer25 // process in Electron. This cannot be tested since we don't run tests in26 // Electron.Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 27–30why, one step back27 // istanbul ignore else28 if (t.unref) {29 t.unref();30 }Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 31–62why, one step back31};3233const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {34 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;35};3637const isSigterm = signal => {38 return signal === os.constants.signals.SIGTERM ||39 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');40};4142const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {43 if (forceKillAfterTimeout === true) {44 return DEFAULT_FORCE_KILL_TIMEOUT;45 }4647 if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {48 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);49 }5051 return forceKillAfterTimeout;52};5354// `childProcess.cancel()`55const spawnedCancel = (spawned, context) => {56 const killResult = spawned.kill();5758 if (killResult) {59 context.isCanceled = true;60 }61};62Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 63–67why, one step back63const timeoutKill = (spawned, signal, reject) => {64 spawned.kill(signal);65 reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));66};67Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 68why, one step back68// `timeout` option handlingRefactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 69–71why, one step back69const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {70 if (timeout === 0 || timeout === undefined) {71 return spawnedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 72why, one step back72 }Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 73–84why, one step back7374 if (!Number.isInteger(timeout) || timeout < 0) {75 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);76 }7778 let timeoutId;79 const timeoutPromise = new Promise((resolve, reject) => {80 timeoutId = setTimeout(() => {81 timeoutKill(spawned, killSignal, reject);82 }, timeout);83 });84Require Node.js 10Sindre Sorhusbefore adoption5a9c76f
line 85why, one step back85 const safeSpawnedPromise = spawnedPromise.finally(() => {Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 86–89why, one step back86 clearTimeout(timeoutId);87 });8889 return Promise.race([timeoutPromise, safeSpawnedPromise]);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 90–92why, one step back90};9192// `cleanup` option handlingRequire Node.js 10Sindre Sorhusbefore adoption5a9c76f
line 93why, one step back93const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 94why, one step back94 if (!cleanup || detached) {Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd
line 95why, one step back95 return timedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 96–97why, one step back96 }97Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 98why, one step back98 const removeExitHandler = onExit(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 99–101why, one step back99 spawned.kill();100 });101Require Node.js 10Sindre Sorhusbefore adoption5a9c76f
lines 102–104why, one step back102 return timedPromise.finally(() => {103 removeExitHandler();104 });Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 105–110why, one step back105};106107module.exports = {108 spawnedKill,109 spawnedCancel,110 setupTimeout,Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 111why, one step back111 setExitHandlerSeparate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 112why, one step back112};