From c1af9c0335d15b56448972854b99da04f71faad5 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Tue, 5 Aug 2025 16:39:48 +0300 Subject: [PATCH] fix(npm): child process exits gracefully on SIGxxx (#241) Signed-off-by: Marc Nuri --- npm/kubernetes-mcp-server/bin/index.js | 21 ++++++++++++++++++++- pkg/http/http.go | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/npm/kubernetes-mcp-server/bin/index.js b/npm/kubernetes-mcp-server/bin/index.js index ee61c4c..9db14d2 100755 --- a/npm/kubernetes-mcp-server/bin/index.js +++ b/npm/kubernetes-mcp-server/bin/index.js @@ -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); + } +}); diff --git a/pkg/http/http.go b/pkg/http/http.go index 602a400..e5dfb2b 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -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() {