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 fileTake 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 back1import {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 back3import {normalizeSignalArgument} from './signal.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 4why, one step back4import {validateDelay} from './timeout.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–15why, one step back56// 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 }15Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 16why, one step back16 validateDelay(forceKillAfterDelay, 'forceKillAfterDelay');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 17–23why, one step back17 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 back24 {kill, options: {forceKillAfterDelay, killSignal}, onInternalError, context, controller},100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 25–28why, one step back25 signalOrError,26 errorArgument,27) => {28 const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 29why, one step back29 emitKillError(error, onInternalError);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–36why, one step back30 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 back37 context,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 38–47why, one step back38 controller,39 });40 return killResult;41};4243const parseKillArguments = (signalOrError, errorArgument, killSignal) => {44 const [signal = killSignal, error] = isErrorInstance(signalOrError)45 ? [undefined, signalOrError]46 : [signalOrError, errorArgument];47Improve validation and typing of signals (#1025)ehmickybefore adoption7213287
lines 48–49why, one step back48 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 back50 }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 }55Improve validation and typing of signals (#1025)ehmickybefore adoption7213287
line 56why, one step back56 return {signal: normalizeSignalArgument(signal), error};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 57–58why, one step back57};58Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
lines 59–62why, one step back59// 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 back63 if (error !== undefined) {Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 64why, one step back64 onInternalError.reject(error);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 65–67why, one step back65 }66};67Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 68–81why, one step back68const 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 back82 return;83 }8485 try {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 86–89why, one step back86 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 back90 } catch {}91};