Skip to content

Commit 4e7a652

Browse files
committed
fix: prevent killPromise from hanging indefinitely
- Add .catch() handler on child.exited promise - Ensure setTimeout always calls resolve() after SIGKILL - Wrap child.kill() in try-catch for error handling - Match the pattern used in cleanup() function This prevents the rebuild from freezing if the process doesn't respond to signals or if child.exited rejects.
1 parent b095993 commit 4e7a652

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

scripts/dev-watch.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,43 @@ async function rebuildAndRestartServer() {
338338

339339
// Use exit event to properly track process termination
340340
const killPromise = new Promise((resolve) => {
341+
// Set up timeout for force kill
342+
const forceKillTimeout = setTimeout(() => {
343+
if (child.exitCode === null) {
344+
log('REBUILD', 'Server did not stop gracefully, force killing...');
345+
try {
346+
child.kill('SIGKILL');
347+
} catch (error) {
348+
// Ignore errors during force kill
349+
}
350+
}
351+
resolve(true); // Always resolve after timeout
352+
}, 500);
353+
354+
// Listen for the process to exit
341355
child.exited.then(() => {
356+
clearTimeout(forceKillTimeout);
342357
log('REBUILD', 'Server stopped gracefully');
343358
resolve(true);
359+
}).catch(() => {
360+
// Handle rejection from child.exited
361+
clearTimeout(forceKillTimeout);
362+
resolve(true);
344363
});
345364

346365
// Try graceful shutdown first
347-
child.kill('SIGTERM');
348-
349-
// Force kill after timeout
350-
setTimeout(() => {
351-
if (child.exitCode === null) {
352-
log('REBUILD', 'Server did not stop gracefully, force killing...');
366+
try {
367+
child.kill('SIGTERM');
368+
} catch (error) {
369+
// If kill fails, try SIGKILL immediately
370+
try {
353371
child.kill('SIGKILL');
372+
} catch (killError) {
373+
// Process might already be dead
354374
}
355-
}, 500);
375+
clearTimeout(forceKillTimeout);
376+
resolve(true);
377+
}
356378
});
357379

358380
await killPromise;

0 commit comments

Comments
 (0)