19 lines
769 B
JavaScript
19 lines
769 B
JavaScript
import { setTimeout as delay } from 'node:timers/promises';
|
|
|
|
// Only handles child objects created by this process. No port/name-based kills.
|
|
export async function stopOwnedChild(child, { graceful = false, timeout = 20000 } = {}) {
|
|
if (!child || child.exitCode !== null || child.signalCode !== null) return;
|
|
const exited = new Promise(resolve => child.once('exit', resolve));
|
|
if (graceful && child.stdin && !child.stdin.destroyed) {
|
|
child.stdin.on('error', () => {});
|
|
child.stdin.end();
|
|
} else {
|
|
child.kill();
|
|
}
|
|
await Promise.race([exited, delay(timeout, undefined, { ref: false })]);
|
|
if (child.exitCode === null && child.signalCode === null) {
|
|
child.kill();
|
|
await Promise.race([exited, delay(3000, undefined, { ref: false })]);
|
|
}
|
|
}
|