Skip to content

Commit

Permalink
Update stopChildProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Feb 5, 2024
1 parent 41393fe commit 8b4928c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/test-utils/src/childProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ export async function execChildProcess(cmd: string | string[], options?: ExecChi
});
}

/**
* Check if process with given pid is running
*/
export function isPidRunning(pid: number): boolean {
try {
// Signal 0 is a special signal that checks if the process exists
process.kill(pid, 0);
return true;
} catch {
return false;
}
}

export const stopChildProcess = async (
childProcess: childProcess.ChildProcess,
signal: NodeJS.Signals | number = "SIGTERM"
Expand All @@ -87,11 +100,20 @@ export const stopChildProcess = async (
return;
}

return new Promise((resolve, reject) => {
const pid = childProcess.pid;

await new Promise((resolve, reject) => {
childProcess.once("error", reject);
childProcess.once("close", resolve);
// We use `exit` instead of `close` as multiple processes can share same `stdio`
childProcess.once("exit", resolve);
childProcess.kill(signal);
});

if (pid != null && isPidRunning(pid)) {
// Wait for sometime and try to kill this time
await sleep(500);
await stopChildProcess(childProcess, "SIGKILL");
}
};

/**
Expand Down

0 comments on commit 8b4928c

Please sign in to comment.