Skip to content

Commit

Permalink
Document how to terminate hanging subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 4, 2024
1 parent abd9fd0 commit 6d8f060
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/termination.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ If the subprocess is still alive after 5 seconds, it is forcefully terminated wi

## Timeout

### Execution timeout

If the subprocess lasts longer than the [`timeout`](api.md#optionstimeout) option, a [`SIGTERM` signal](#default-signal) is sent to it.

```js
Expand All @@ -138,6 +140,36 @@ try {
}
```

### Inactivity timeout

To terminate a subprocess when it becomes inactive, the [`cancelSignal`](#canceling) option can be combined with 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.

```js
import {execa} from 'execa';
import debounceFn from 'debounce-fn';

// 1 minute
const wait = 60_000;
const controller = new AbortController();
const cancelSignal = controller.signal;
const abort = debounceFn(controller.abort.bind(controller), {wait});
const onOutput = {
* transform(data) {
abort();
yield data;
},
binary: true,
};

abort();
await execa({
cancelSignal,
stdout: onOutput,
stderr: onOutput,
})`npm run build`;

```

## Current process exit

If the current process exits, the subprocess is automatically [terminated](#default-signal) unless either:
Expand Down

0 comments on commit 6d8f060

Please sign in to comment.