cli: gracefully shut down child process on windows

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-01 15:35:04 +02:00
parent afa48341fb
commit d0f26cfa4f
4 changed files with 20 additions and 1 deletions
@@ -18,6 +18,7 @@ import { FSWatcher, watch } from 'chokidar';
import { BackendServeOptions } from '../bundler/types';
import type { ChildProcess } from 'child_process';
import { ctrlc } from 'ctrlc-windows';
import { IpcServer } from './IpcServer';
import { ServerDataStore } from './ServerDataStore';
import debounce from 'lodash/debounce';
@@ -57,7 +58,11 @@ export async function startBackendExperimental(options: BackendServeOptions) {
if (child && !child.killed && child.exitCode === null) {
// We always wait for the existing process to exit, to make sure we don't get IPC conflicts
shutdownPromise = new Promise(resolve => child!.once('exit', resolve));
child.kill();
if (process.platform === 'win32' && child.pid) {
ctrlc(child.pid);
} else {
child.kill();
}
await shutdownPromise;
shutdownPromise = undefined;
}