/** * @file Cross-platform process-tree termination. A spawned package manager, * proxy, or build tool spawns its own children; killing only the direct child * orphans them (they reparent to init on POSIX and run forever). These * helpers kill the whole tree: * * - POSIX, `detached: true` (default): the child leads its own process group, * so `process.kill(-pid, signal)` signals every member at once. * - POSIX, `detached: false`: there is no group to signal, so the process table * is snapshotted once and every descendant is signalled individually. A * non-detached child is the common case — Node's own `timeout` option * signals only the direct child — so without this walk a timed-out spawn * leaves its whole subtree alive and reparented to init. * - Windows: there are no POSIX process groups, so we shell out to `taskkill /T * /F /pid `, which walks and terminates the descendant tree. The * `signal` argument is ignored on Windows (taskkill is always a forceful * terminate). Both helpers are best-effort and never throw: a process that * already exited (ESRCH) or that we lack permission to signal (EPERM) is * treated as "nothing to do", because a cleanup kill must not mask the * caller's original control flow. `killProcessTree` returns `true` if a * kill was attempted, `false` if the pid was invalid or already gone. */ import type { ChildProcess } from 'node:child_process'; export interface KillProcessTreeOptions { /** * POSIX only. When `true` (default), signal the child's entire process group * via the negative pid — requires the child to have been spawned `detached: * true`. When `false`, signal only the single pid. Ignored on Windows, where * taskkill always kills the tree. */ detached?: boolean | undefined; /** * POSIX signal to send (default `'SIGTERM'`). Ignored on Windows, where * taskkill performs a forceful terminate. */ signal?: NodeJS.Signals | number | undefined; } /** * Every descendant of `pid`, deepest-last, from a `pid -> ppid` snapshot. * * Why a snapshot rather than repeated `pgrep -P`: a process that exits during * the walk reparents its children to init, and they would vanish from a * later query while still holding memory. Reading the table once fixes the * shape of the tree before anything is signalled. * * The visited set doubles as a cycle guard: a corrupt table must not hang a * cleanup path. */ export declare function collectDescendantPids(pid: number, parents: ReadonlyMap): number[]; /** * Probe whether a pid is still alive. Uses signal 0, which performs the * permission/existence check without delivering a signal. Returns `false` for * pid <= 1 (kernel/init) so callers don't mistake those for a live child. */ export declare function isProcessAlive(pid: number): boolean; /** * Kill a process and its descendants. See the file header for the * POSIX-process-group vs. Windows-taskkill strategy. Best-effort: never throws. * Returns `true` if a kill was attempted, `false` if the pid was invalid or the * process had already exited. */ export declare function killProcessTree(target: number | ChildProcess, options?: KillProcessTreeOptions | undefined): boolean; /** * Snapshot the POSIX process table as `pid -> ppid`. * * Delegates to {@link readProcessTree}, the package's one process-table * reader, rather than running its own `ps`. Two readers in one package drift: * this one shipped without the `maxBuffer` bump that a full table on a busy * host needs, and would have silently truncated where the other does not. * * Returns an empty map when `ps` is unavailable, which makes the caller fall * back to signalling the single pid — never to signalling nothing it did not * mean to. */ export declare function readParentMap(): Map; /** * Resolve a pid from either a raw number or a spawned ChildProcess. Returns * `undefined` for a missing/invalid pid or a process that already exited. * Exported for direct testing. */ export declare function resolvePid(target: number | ChildProcess): number | undefined;