fix(npm): child process exits gracefully on SIGxxx (#241)

Signed-off-by: Marc Nuri <marc@marcnuri.com>
This commit is contained in:
Marc Nuri
2025-08-05 16:39:48 +03:00
committed by GitHub
parent 29b65fd565
commit c1af9c0335
2 changed files with 21 additions and 2 deletions

View File

@@ -21,7 +21,26 @@ const resolveBinaryPath = () => {
}
};
childProcess.execFileSync(resolveBinaryPath(), process.argv.slice(2), {
const child = childProcess.spawn(resolveBinaryPath(), process.argv.slice(2), {
stdio: 'inherit',
});
const handleSignal = () => (signal) => {
console.log(`Received ${signal}, terminating child process...`);
if (child && !child.killed) {
child.kill(signal);
}
};
['SIGTERM', 'SIGINT', 'SIGHUP'].forEach((signal) => {
process.on(signal, handleSignal(signal));
});
child.on('close', (code, signal) => {
if (signal) {
console.log(`Child process terminated by signal: ${signal}`);
process.exit(128 + (signal === 'SIGTERM' ? 15 : signal === 'SIGINT' ? 2 : 1));
} else {
process.exit(code || 0);
}
});

View File

@@ -84,7 +84,7 @@ func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.Stat
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
serverErr := make(chan error, 1)
go func() {