MCPcopy Create free account
hub / github.com/tiann/hapi / waitForProcessToDie

Function waitForProcessToDie

cli/src/utils/process.ts:121–144  ·  view source on GitHub ↗

* Waits for a process to die, escalating to SIGKILL if SIGTERM doesn't work.

(pid: number, force: boolean)

Source from the content-addressed store, hash-verified

119 * Waits for a process to die, escalating to SIGKILL if SIGTERM doesn't work.
120 */
121async function waitForProcessToDie(pid: number, force: boolean): Promise<void> {
122 const maxWait = 2000;
123 const pollInterval = 20;
124 let waited = 0;
125
126 while (isProcessAlive(pid) && waited < maxWait) {
127 await new Promise(r => setTimeout(r, pollInterval));
128 waited += pollInterval;
129 }
130
131 // If SIGTERM didn't work and we haven't tried SIGKILL yet, escalate
132 if (!force && isProcessAlive(pid)) {
133 try {
134 process.kill(pid, 'SIGKILL');
135 } catch {
136 return;
137 }
138 waited = 0;
139 while (isProcessAlive(pid) && waited < 1000) {
140 await new Promise(r => setTimeout(r, pollInterval));
141 waited += pollInterval;
142 }
143 }
144}
145
146export async function killProcessByChildProcess(
147 child: ChildProcess,

Callers 2

killProcessFunction · 0.85
killProcessTreeFunction · 0.85

Calls 1

isProcessAliveFunction · 0.70

Tested by

no test coverage detected