Skip to content

Commit 6389bb0

Browse files
committed
Ensure the server is reliably killed on windows
1 parent 98f737b commit 6389bb0

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/index.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function reportError(error: Error | string) {
99
}
1010
}
1111

12-
import { spawn, ChildProcess } from 'child_process';
12+
import { spawn, exec, ChildProcess } from 'child_process';
1313
import * as os from 'os';
1414
import * as path from 'path';
1515
import { app, BrowserWindow, shell, Menu } from 'electron';
@@ -93,16 +93,33 @@ app.on('window-all-closed', () => {
9393
});
9494

9595
let serverKilled = false;
96-
app.on('quit', () => {
97-
if (server && !isWindows) {
98-
// On windows, children die automatically.
99-
// Elsewhere, we have to make sure we clean up the whole group.
100-
// https://azimi.me/2014/12/31/kill-child_process-node-js.html
96+
app.on('will-quit', (event) => {
97+
if (server && !serverKilled) {
98+
serverKilled = true;
10199
try {
102-
serverKilled = true;
103-
process.kill(-server.pid);
104-
} catch (e) {
105-
console.log(e);
100+
if (isWindows) {
101+
// Don't shutdown until we've tried to kill the server
102+
event.preventDefault();
103+
104+
// Forcefully kill the pid (the cmd) and child processes
105+
exec(`taskkill /pid ${server.pid} /T /F`, (error, stdout, stderr) => {
106+
if (error) {
107+
console.log(stdout);
108+
console.log(stderr);
109+
reportError(error);
110+
}
111+
112+
// We've done our best - now shut down for real
113+
app.quit();
114+
});
115+
} else {
116+
// Make sure we clean up the whole group (shell + node).
117+
// https://azimi.me/2014/12/31/kill-child_process-node-js.html
118+
process.kill(-server.pid);
119+
}
120+
} catch (error) {
121+
console.log('Failed to kill server', error);
122+
reportError(error);
106123
}
107124
}
108125
});

0 commit comments

Comments
 (0)