Skip to content
Myr

kill.js

This file

Path

lib/terminate/kill.js

91 lines · 2.7 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–2why, one step back
1import {setTimeout} from 'node:timers/promises';
2import {isErrorInstance} from '../return/final-error.js';

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 3why, one step back
3import {normalizeSignalArgument} from './signal.js';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 4why, one step back
4import {validateDelay} from './timeout.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 5–15why, one step back
5
6// Normalize the `forceKillAfterDelay` option7export const normalizeForceKillAfterDelay = forceKillAfterDelay => {8 if (forceKillAfterDelay === false) {9 return forceKillAfterDelay;10 }1112 if (forceKillAfterDelay === true) {13 return DEFAULT_FORCE_KILL_TIMEOUT;14 }15

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 16why, one step back
16 validateDelay(forceKillAfterDelay, 'forceKillAfterDelay');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 17–23why, one step back
17 return forceKillAfterDelay;
18};1920const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;2122// Monkey-patches `subprocess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`23export const subprocessKill = (

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 24why, one step back
24 {kill, options: {forceKillAfterDelay, killSignal}, onInternalError, context, controller},

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 25–28why, one step back
25 signalOrError,
26 errorArgument,27) => {28 const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 29why, one step back
29 emitKillError(error, onInternalError);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 30–36why, one step back
30 const killResult = kill(signal);
31 setKillTimeout({32 kill,33 signal,34 forceKillAfterDelay,35 killSignal,36 killResult,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 37why, one step back
37 context,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–47why, one step back
38 controller,
39 });40 return killResult;41};4243const parseKillArguments = (signalOrError, errorArgument, killSignal) => {44 const [signal = killSignal, error] = isErrorInstance(signalOrError)45 ? [undefined, signalOrError]46 : [signalOrError, errorArgument];47

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 48–49why, one step back
48 if (typeof signal !== 'string' && !Number.isInteger(signal)) {
49 throw new TypeError(`The first argument must be an error instance or a signal name string/integer: ${String(signal)}`);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 50–55why, one step back
50 }
5152 if (error !== undefined && !isErrorInstance(error)) {53 throw new TypeError(`The second argument is optional. If specified, it must be an error instance: ${error}`);54 }55

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 56why, one step back
56 return {signal: normalizeSignalArgument(signal), error};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 57–58why, one step back
57};
58

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

lines 59–62why, one step back
59// Fails right away when calling `subprocess.kill(error)`.
60// Does not wait for actual signal termination.61// Uses a deferred promise instead of the `error` event on the subprocess, as this is less intrusive.62const emitKillError = (error, onInternalError) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 63why, one step back
63 if (error !== undefined) {

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 64why, one step back
64 onInternalError.reject(error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 65–67why, one step back
65 }
66};67

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 68–81why, one step back
68const setKillTimeout = async ({kill, signal, forceKillAfterDelay, killSignal, killResult, context, controller}) => {
69 if (signal === killSignal && killResult) {70 killOnTimeout({71 kill,72 forceKillAfterDelay,73 context,74 controllerSignal: controller.signal,75 });76 }77};7879// Forcefully terminate a subprocess after a timeout80export const killOnTimeout = async ({kill, forceKillAfterDelay, context, controllerSignal}) => {81 if (forceKillAfterDelay === false) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 82–85why, one step back
82 return;
83 }8485 try {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 86–89why, one step back
86 await setTimeout(forceKillAfterDelay, undefined, {signal: controllerSignal});
87 if (kill('SIGKILL')) {88 context.isForcefullyTerminated ??= true;89 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 90–91why, one step back
90 } catch {}
91};