Skip to content
Myr

Document how to terminate hanging subprocesses (#1140)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent abd9fd0 · Browse files · 1 file+47 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+47 added−0 removed

  • other:+47 added−0 removed

1 file+47 added−0 removed

modified:docs/termination.md+47 added
122 unmodified lines
123If the subprocess is still alive after 5 seconds, it is forcefully terminated with [`SIGKILL`](#sigkill). This can be [configured or disabled](#forceful-termination) using the [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option.124125## Timeout126127### Execution timeout128129If the subprocess lasts longer than the [`timeout`](api.md#optionstimeout) option, a [`SIGTERM` signal](#default-signal) is sent to it.130131```js132try {
6 unmodified lines
139 throw error;140}141```142143### Inactivity timeout144145To terminate a subprocess when it becomes inactive, the [`cancelSignal`](#canceling) option can be combined with [transforms](transform.md) and some [debouncing logic](https://github.com/sindresorhus/debounce-fn). The following example terminates the subprocess if it has not printed to [`stdout`](api.md#resultstdout)/[`stderr`](api.md#resultstderr) in the last minute.146147```js148import {execa} from 'execa';149import debounceFn from 'debounce-fn';150151// 1 minute152const wait = 60_000;153154const getInactivityOptions = () => {155 const controller = new AbortController();156 const cancelSignal = controller.signal;157158 // Delay and debounce `cancelSignal` each time `controller.abort()` is called159 const scheduleAbort = debounceFn(controller.abort.bind(controller), {wait});160161 const onOutput = {162 * transform(data) {163 // When anything is printed, debounce `controller.abort()`164 scheduleAbort();165166 // Keep the output as is167 yield data;168 },169 // Debounce even if the output does not include any newline170 binary: true,171 };172173 // Start debouncing174 scheduleAbort();175176 return {177 cancelSignal,178 stdout: onOutput,179 stderr: onOutput,180 };181};182183const options = getInactivityOptions();184185await execa(options)`npm run build`;186```187188## Current process exit189190If the current process exits, the subprocess is automatically [terminated](#default-signal) unless either:191- The [`cleanup`](api.md#optionscleanup) option is `false`.
167 unmodified lines