Skip to content
Myr

kill.js

This file

Path

lib/kill.js

103 lines · 3.0 KiB · JavaScript

Last changed by

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

History of this file

Take it

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 1why, one step back
1import os from 'node:os';

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 2why, one step back
2import {setTimeout as pSetTimeout} from 'node:timers/promises';

Upgrade `signal-exit` to v4 (#570)ehmickybefore adoption468fbe1

line 3why, one step back
3import {onExit} from 'signal-exit';

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

lines 4–7why, one step back
4
5const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;67// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 8why, one step back
8export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {

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

lines 9–19why, one step back
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–23why, one step back
22 }, timeout);
23

Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405

lines 24–26why, one step back
24 // Guarded because there's no `.unref()` when `execa` is used in the renderer
25 // 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 back
27 // istanbul ignore else
28 if (t.unref) {29 t.unref();30 }

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

lines 31–32why, one step back
31};
32

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 33why, one step back
33const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;

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

line 34why, one step back
34

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 35–36why, one step back
35const isSigterm = signal => signal === os.constants.signals.SIGTERM
36 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');

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

lines 37–42why, one step back
37
38const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {39 if (forceKillAfterTimeout === true) {40 return DEFAULT_FORCE_KILL_TIMEOUT;41 }42

Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3

line 43why, one step back
43 if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {

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

lines 44–50why, one step back
44 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);
45 }4647 return forceKillAfterTimeout;48};4950// `childProcess.cancel()`

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 51why, one step back
51export const spawnedCancel = (spawned, context) => {

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

lines 52–58why, one step back
52 const killResult = spawned.kill();
5354 if (killResult) {55 context.isCanceled = true;56 }57};58

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 59–62why, one step back
59const killAfterTimeout = async (spawned, timeout, killSignal, controller) => {
60 await pSetTimeout(timeout, undefined, {ref: false, signal: controller.signal});61 spawned.kill(killSignal);62 throw Object.assign(new Error('Timed out'), {timedOut: true, signal: killSignal});

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

lines 63–64why, one step back
63};
64

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

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

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 66why, one step back
66export const setupTimeout = async (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {

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

lines 67–68why, one step back
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

line 70why, one step back
70

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 71–79why, one step back
71 const controller = new AbortController();
72 try {73 return await Promise.race([74 spawnedPromise,75 killAfterTimeout(spawned, timeout, killSignal, controller),76 ]);77 } finally {78 controller.abort();79 }

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

lines 80–81why, one step back
80};
81

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 82why, one step back
82export const validateTimeout = ({timeout}) => {

Fix `timeout` option validation (#463)ehmickybefore adoption427c5c2

lines 83–87why, one step back
83 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {
84 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);85 }86};87

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

line 88why, one step back
88// `cleanup` option handling

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

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

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

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

line 91why, one step back
91 return timedPromise;

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

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

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

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

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

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

Refactor `cleanup` option to use `async/await` (#649)ehmickybefore adoption813e2c6

lines 98–100why, one step back
98 try {
99 return await timedPromise;100 } finally {

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

line 101why, one step back
101 removeExitHandler();

Refactor `cleanup` option to use `async/await` (#649)ehmickybefore adoption813e2c6

line 102why, one step back
102 }

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

line 103why, one step back
103};