fix(daemon): Ensure robust process shutdown and improve logs/subscriber diagnostics

This commit is contained in:
2025-08-31 00:01:50 +00:00
parent 4160b3f031
commit b3087831e2
7 changed files with 98 additions and 17 deletions

View File

@@ -180,7 +180,7 @@ export class ProcessWrapper extends EventEmitter {
/**
* Stop the wrapped process
*/
public stop(): void {
public async stop(): Promise<void> {
if (!this.process) {
this.logger.debug('Stop called but no process is running');
this.addSystemLog('No process running');
@@ -194,11 +194,32 @@ export class ProcessWrapper extends EventEmitter {
if (this.process.pid) {
try {
this.logger.debug(`Sending SIGTERM to process ${this.process.pid}`);
process.kill(this.process.pid, 'SIGTERM');
try {
// Try to signal the whole process group on POSIX to ensure children get the signal too
if (process.platform !== 'win32') {
process.kill(-Math.abs(this.process.pid), 'SIGTERM');
} else {
process.kill(this.process.pid, 'SIGTERM');
}
} catch {
// Fallback to direct process kill if group kill fails
process.kill(this.process.pid, 'SIGTERM');
}
// Give it 5 seconds to shut down gracefully
setTimeout((): void => {
if (this.process && this.process.pid) {
// Wait for exit or escalate
await new Promise<void>((resolve) => {
let settled = false;
const cleanup = () => {
if (settled) return;
settled = true;
resolve();
};
const onExit = () => cleanup();
this.process!.once('exit', onExit);
const killTimer = setTimeout(() => {
if (!this.process || !this.process.pid) return cleanup();
this.logger.warn(
`Process ${this.process.pid} did not exit gracefully, force killing...`,
);
@@ -206,17 +227,27 @@ export class ProcessWrapper extends EventEmitter {
'Process did not exit gracefully, force killing...',
);
try {
process.kill(this.process.pid, 'SIGKILL');
} catch (error: Error | unknown) {
// Process might have exited between checks
if (process.platform !== 'win32') {
process.kill(-Math.abs(this.process.pid), 'SIGKILL');
} else {
process.kill(this.process.pid, 'SIGKILL');
}
} catch (error: any) {
this.logger.debug(
`Failed to send SIGKILL, process probably already exited: ${
error instanceof Error ? error.message : String(error)
}`,
`Failed to send SIGKILL, process probably already exited: ${error?.message || String(error)}`,
);
}
}
}, 5000);
// Give a short grace period after SIGKILL
setTimeout(() => cleanup(), 500);
}, 5000);
// Safety cap in case neither exit nor timer fires (shouldn't happen)
setTimeout(() => {
clearTimeout(killTimer);
cleanup();
}, 10000);
});
} catch (error: Error | unknown) {
const processError = new ProcessError(
error instanceof Error ? error.message : String(error),