UNPKG

histoire

Version:

A fast story book powered by Vite

96 lines (95 loc) • 4.12 kB
import { spawn } from 'node:child_process'; import { win32 } from 'node:path'; /** Repeated stops join the same shutdown and never signal a reused PID. */ const stopping = new WeakMap(); /** Wait for lifecycle evidence without letting inherited pipes block forever. */ async function settles(operation, timeout) { let timer; try { return await Promise.race([operation.then(() => true), new Promise(resolve => timer = setTimeout(() => resolve(false), timeout))]); } finally { clearTimeout(timer); } } /** Stop an owned detached child and its descendants, escalating before closing pipes. */ export function stopProcessTree(child, timeouts = {}) { const previous = stopping.get(child); if (previous) return previous; const operation = stop(); stopping.set(child, operation); return operation; /** Signal only the process group minted by this child, even after leader exit. */ function signalGroup(signal) { if (!child.pid) return false; try { process.kill(-child.pid, signal); return true; } catch (error) { if (error.code === 'ESRCH') return false; throw error; } } /** Escalate descendants independently of leader lifecycle and inherited pipes. */ async function stop() { let ended = child.exitCode !== null || child.signalCode !== null || !child.pid; let exited; let closed; const exit = new Promise(resolve => exited = resolve); const close = new Promise(resolve => closed = resolve); /** Both spawn errors and exit events settle direct process ownership. */ function onExit() { ended = true; exited(); } child.once('exit', onExit); child.once('error', onExit); child.once('close', closed); try { if (process.platform === 'win32') { if (child.pid && !ended) { const killer = spawn(win32.join(timeouts.systemRoot ?? process.env.SystemRoot ?? 'C:\\Windows', 'System32/taskkill.exe'), ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true, shell: false, timeout: timeouts.windows ?? 2000 }); await new Promise((resolve) => { killer.once('error', () => resolve()); killer.once('close', () => resolve()); }); if (!ended) child.kill('SIGKILL'); } } else { let alive = signalGroup('SIGTERM'); const deadline = Date.now() + (timeouts.terminate ?? 1000); while (alive && Date.now() < deadline) { await new Promise(resolve => setTimeout(resolve, timeouts.poll ?? 20)); alive = signalGroup(0); } if (alive) { signalGroup('SIGKILL'); const killedDeadline = Date.now() + (timeouts.killed ?? 250); // Orphaned zombies can remain visible until the OS reaps them. while (signalGroup(0) && Date.now() < killedDeadline) await new Promise(resolve => setTimeout(resolve, timeouts.poll ?? 20)); } } const terminated = ended || await settles(exit, timeouts.exit ?? 1000); // Let ordinary output drain before forcing inherited pipes closed. await settles(close, timeouts.close ?? 250); child.stdin?.destroy(); child.stdout?.destroy(); child.stderr?.destroy(); await settles(close, timeouts.close ?? 250); if (!terminated) throw new Error(timeouts.failureMessage ?? 'Process did not exit after forced termination'); } finally { child.removeListener('exit', onExit); child.removeListener('error', onExit); child.removeListener('close', closed); } } }