Skip to content

Commit c1af9c0

Browse files
authored
fix(npm): child process exits gracefully on SIGxxx (#241)
Signed-off-by: Marc Nuri <[email protected]>
1 parent 29b65fd commit c1af9c0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

npm/kubernetes-mcp-server/bin/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ const resolveBinaryPath = () => {
2121
}
2222
};
2323

24-
childProcess.execFileSync(resolveBinaryPath(), process.argv.slice(2), {
24+
const child = childProcess.spawn(resolveBinaryPath(), process.argv.slice(2), {
2525
stdio: 'inherit',
2626
});
2727

28+
const handleSignal = () => (signal) => {
29+
console.log(`Received ${signal}, terminating child process...`);
30+
if (child && !child.killed) {
31+
child.kill(signal);
32+
}
33+
};
34+
35+
['SIGTERM', 'SIGINT', 'SIGHUP'].forEach((signal) => {
36+
process.on(signal, handleSignal(signal));
37+
});
38+
39+
child.on('close', (code, signal) => {
40+
if (signal) {
41+
console.log(`Child process terminated by signal: ${signal}`);
42+
process.exit(128 + (signal === 'SIGTERM' ? 15 : signal === 'SIGINT' ? 2 : 1));
43+
} else {
44+
process.exit(code || 0);
45+
}
46+
});

pkg/http/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.Stat
8484
defer cancel()
8585

8686
sigChan := make(chan os.Signal, 1)
87-
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
87+
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
8888

8989
serverErr := make(chan error, 1)
9090
go func() {

0 commit comments

Comments
 (0)