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 40b008f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 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,27 @@ try {
}
```

### Inactivity timeout

To terminate a subprocess when it is hanging, 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;
const controller = new AbortController();
const cancelSignal = controller.signal;
const abort = debounceFn(controller.abort.bind(controller), {wait});

const subprocess = execa({cancelSignal})`npm run build`;
abort();
subprocess.stdout.on('data', abort);
subprocess.stderr.on('data', abort);
await subprocess;
```

## Current process exit

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

0 comments on commit 40b008f

Please sign in to comment.