fix: destroy both stdin and stdout on shutdown, guard against duplicate signals

- Close stdout in addition to stdin to prevent hanging writes during shutdown
- Add shuttingDown guard to ensure cleanup runs only once

Made-with: Cursor
This commit is contained in:
沐目 2026-03-19 11:53:13 +08:00
parent 2e6ab6562b
commit a6e8aec7f4

View file

@ -91,13 +91,21 @@ export async function runAcpAgent(
// 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);