Skip to content
Myr

kill.js

This file

Path

lib/kill.js

104 lines · 2.8 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–3why, one step back
1'use strict';
2const os = require('os');3const onExit = require('signal-exit');

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

line 4why, one step back
4const pFinally = require('p-finally');

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

lines 5–55why, one step back
5
6const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;78// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior9const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {10 const killResult = kill(signal);11 setKillTimeout(kill, signal, options, killResult);12 return killResult;13};1415const setKillTimeout = (kill, signal, options, killResult) => {16 if (!shouldForceKill(signal, options, killResult)) {17 return;18 }1920 const timeout = getForceKillAfterTimeout(options);21 setTimeout(() => {22 kill('SIGKILL');23 }, timeout).unref();24};2526const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {27 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;28};2930const isSigterm = signal => {31 return signal === os.constants.signals.SIGTERM ||32 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');33};3435const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {36 if (forceKillAfterTimeout === true) {37 return DEFAULT_FORCE_KILL_TIMEOUT;38 }3940 if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {41 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);42 }4344 return forceKillAfterTimeout;45};4647// `childProcess.cancel()`48const spawnedCancel = (spawned, context) => {49 const killResult = spawned.kill();5051 if (killResult) {52 context.isCanceled = true;53 }54};55

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

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

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

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

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

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

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

line 65why, one step back
65 }

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

lines 66–82why, one step back
66
67 if (!Number.isInteger(timeout) || timeout < 0) {68 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);69 }7071 let timeoutId;72 const timeoutPromise = new Promise((resolve, reject) => {73 timeoutId = setTimeout(() => {74 timeoutKill(spawned, killSignal, reject);75 }, timeout);76 });7778 const safeSpawnedPromise = pFinally(spawnedPromise, () => {79 clearTimeout(timeoutId);80 });8182 return Promise.race([timeoutPromise, safeSpawnedPromise]);

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

lines 83–85why, one step back
83};
8485// `cleanup` option handling

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

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

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

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

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

line 88why, one step back
88 return timedPromise;

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

lines 89–90why, one step back
89 }
90

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

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

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

lines 92–94why, one step back
92 spawned.kill();
93 });94

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

lines 95–96why, one step back
95 // TODO: Use native "finally" syntax when targeting Node.js 10
96 return pFinally(timedPromise, removeExitHandler);

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

lines 97–102why, one step back
97};
9899module.exports = {100 spawnedKill,101 spawnedCancel,102 setupTimeout,

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

line 103why, one step back
103 setExitHandler

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

line 104why, one step back
104};