refactor launchedProcess.killed

This commit is contained in:
Simon Knott 2024-11-07 12:12:02 +01:00
parent 4c881cf11c
commit 220ed29dbe
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 4 additions and 8 deletions

View file

@ -215,18 +215,18 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
}
gracefullyClosing = true;
options.log(`[pid=${spawnedProcess.pid}] <gracefully close start>`);
await options.attemptToGracefullyClose().catch(() => killProcess());
await options.attemptToGracefullyClose().catch(() => killProcess(true));
await waitForCleanup; // Ensure the process is dead and we have cleaned up.
options.log(`[pid=${spawnedProcess.pid}] <gracefully close end>`);
}
// This method has to be sync to be used in the 'exit' event handler.
function killProcess() {
function killProcess(evenIfAlreadyKilled = false) {
gracefullyCloseSet.delete(gracefullyClose);
killSet.delete(killProcessAndCleanup);
removeProcessHandlersIfNeeded();
options.log(`[pid=${spawnedProcess.pid}] <kill>`);
if (spawnedProcess.pid && !spawnedProcess.killed && !processClosed) {
if (spawnedProcess.pid && (evenIfAlreadyKilled || !spawnedProcess.killed) && !processClosed) {
options.log(`[pid=${spawnedProcess.pid}] <will force kill>`);
// Force kill the browser.
try {

View file

@ -130,11 +130,7 @@ export class WebServerPlugin implements TestRunnerPlugin {
return new Promise<void>((resolve, reject) => {
const timer = timeout !== 0
? setTimeout(() => {
// @ts-expect-error. SIGINT didn't kill the process, but `processLauncher` will only attempt killing it if this is false
launchedProcess.killed = false;
reject(new Error(`process didn't close gracefully within timeout, falling back to SIGKILL`));
}, timeout)
? setTimeout(() => reject(new Error(`process didn't close gracefully within timeout, falling back to SIGKILL`)), timeout)
: undefined;
launchedProcess.once('exit', () => {
clearTimeout(timer);