fix: handle SIGTERM/SIGINT in ACP mode for graceful shutdown (#1884)

The ACP process was ignoring SIGTERM because signal handlers registered
elsewhere in the CLI startup path (e.g., stdin raw mode restoration)
override Node.js's default exit behavior. This caused the process to
remain alive after the IDE sends SIGTERM, leading to zombie connections
and exit code 143 errors in JetBrains IntelliJ IDEA.

Add explicit SIGTERM/SIGINT handlers in runAcpAgent() that destroy
stdin, which closes the ndjson stream and resolves connection.closed,
allowing the process to exit gracefully.

Made-with: Cursor
This commit is contained in:
沐目 2026-03-19 11:46:57 +08:00
parent 692e063cd7
commit 2e6ab6562b

View file

@ -87,7 +87,25 @@ export async function runAcpAgent(
stream,
);
// Handle SIGTERM/SIGINT for graceful shutdown.
// Without this, signal handlers registered elsewhere in the CLI
// (e.g., stdin raw mode restoration) override the default exit behavior,
// causing the ACP process to ignore termination signals.
const shutdownHandler = () => {
debugLogger.debug('[ACP] Shutdown signal received, closing streams');
try {
process.stdin.destroy();
} catch {
// stdin may already be closed
}
};
process.on('SIGTERM', shutdownHandler);
process.on('SIGINT', shutdownHandler);
await connection.closed;
process.off('SIGTERM', shutdownHandler);
process.off('SIGINT', shutdownHandler);
}
function toStdioServer(server: McpServer): McpServerStdio | undefined {