Skip to content
Myr

kill.js

This file

Path

lib/kill.js

109 lines · 2.9 KiB · JavaScript

Last changed by

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

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–20why, 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);

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

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

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

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

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

lines 23–28why, one step back
23 }, timeout);
2425 // istanbul ignore else26 if (t.unref) {27 t.unref();28 }

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

lines 29–60why, one step back
29};
3031const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {32 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;33};3435const isSigterm = signal => {36 return signal === os.constants.signals.SIGTERM ||37 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');38};3940const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {41 if (forceKillAfterTimeout === true) {42 return DEFAULT_FORCE_KILL_TIMEOUT;43 }4445 if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {46 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);47 }4849 return forceKillAfterTimeout;50};5152// `childProcess.cancel()`53const spawnedCancel = (spawned, context) => {54 const killResult = spawned.kill();5556 if (killResult) {57 context.isCanceled = true;58 }59};60

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

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

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

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

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

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

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

line 70why, one step back
70 }

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

lines 71–87why, one step back
71
72 if (!Number.isInteger(timeout) || timeout < 0) {73 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);74 }7576 let timeoutId;77 const timeoutPromise = new Promise((resolve, reject) => {78 timeoutId = setTimeout(() => {79 timeoutKill(spawned, killSignal, reject);80 }, timeout);81 });8283 const safeSpawnedPromise = pFinally(spawnedPromise, () => {84 clearTimeout(timeoutId);85 });8687 return Promise.race([timeoutPromise, safeSpawnedPromise]);

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

lines 88–90why, one step back
88};
8990// `cleanup` option handling

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

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

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

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

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

line 93why, one step back
93 return timedPromise;

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

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

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

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

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

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

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

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

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};