Skip to content

Commit

Permalink
Document how to terminate hanging subprocesses (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Aug 7, 2024
1 parent abd9fd0 commit c0b6efc
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 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,51 @@ try {
}
```

### Inactivity timeout

To 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.

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

// 1 minute
const wait = 60_000;

const getInactivityOptions = () => {
const controller = new AbortController();
const cancelSignal = controller.signal;

// Delay and debounce `cancelSignal` each time `controller.abort()` is called
const scheduleAbort = debounceFn(controller.abort.bind(controller), {wait});

const onOutput = {
* transform(data) {
// When anything is printed, debounce `controller.abort()`
scheduleAbort();

// Keep the output as is
yield data;
},
// Debounce even if the output does not include any newline
binary: true,
};

// Start debouncing
scheduleAbort();

return {
cancelSignal,
stdout: onOutput,
stderr: onOutput,
};
};

const options = getInactivityOptions();

await execa(options)`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 c0b6efc

Please sign in to comment.