Merge pull request #2472 from QwenLM/fix/acp-connection-state-cleanup-1780

fix: clean up ACP connection state when child process exits
This commit is contained in:
tanzhenxin 2026-03-20 11:51:14 +08:00 committed by GitHub
commit 572fa81f38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 199 additions and 9 deletions

View file

@ -87,7 +87,33 @@ 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.
let shuttingDown = false;
const shutdownHandler = () => {
if (shuttingDown) return;
shuttingDown = true;
debugLogger.debug('[ACP] Shutdown signal received, closing streams');
try {
process.stdin.destroy();
} catch {
// stdin may already be closed
}
try {
process.stdout.destroy();
} catch {
// stdout 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 {