-
Notifications
You must be signed in to change notification settings - Fork 46
fix(tui): terminate cleanly when terminal closes #610
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -128,11 +128,18 @@ export const TuiThreadCommand = cmd({ | |||||||||||||
| await client.call("reload", undefined) | ||||||||||||||
| }) | ||||||||||||||
| // kilocode_change start - graceful shutdown on external signals | ||||||||||||||
| let shuttingDown = false | ||||||||||||||
| const shutdown = async () => { | ||||||||||||||
| if (shuttingDown) return | ||||||||||||||
| shuttingDown = true | ||||||||||||||
| await client.call("shutdown", undefined).catch(() => {}) | ||||||||||||||
| } | ||||||||||||||
| process.on("SIGHUP", shutdown) | ||||||||||||||
| process.on("SIGTERM", shutdown) | ||||||||||||||
| process.once("SIGHUP", shutdown) | ||||||||||||||
| process.once("SIGTERM", shutdown) | ||||||||||||||
| process.once("SIGINT", shutdown) | ||||||||||||||
|
Comment on lines
+137
to
+139
|
||||||||||||||
| process.once("SIGHUP", shutdown) | |
| process.once("SIGTERM", shutdown) | |
| process.once("SIGINT", shutdown) | |
| process.on("SIGHUP", shutdown) | |
| process.on("SIGTERM", shutdown) | |
| process.on("SIGINT", shutdown) |
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stdin event listeners may cause premature shutdown when stdin is piped. On line 146, Bun.stdin.text() reads stdin completely when !process.stdin.isTTY, which will trigger the "end" event immediately after reading. This means the TUI would shut down right after starting when input is piped.
Consider registering the stdin event handlers only when process.stdin.isTTY is true, or register them after the stdin read at line 146 completes.
| process.stdin.once("end", shutdown) | |
| process.stdin.once("close", shutdown) | |
| if (process.stdin.isTTY) { | |
| process.stdin.once("end", shutdown) | |
| process.stdin.once("close", shutdown) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Handling
SIGINThere may conflict with the TUI's own Ctrl+C handling.The TUI (ink-based) typically intercepts
SIGINTto manage its own exit flow (e.g., prompting to confirm exit, cleaning up the terminal). By registeringprocess.once("SIGINT", shutdown)at this level, the signal handler will callclient.call("shutdown")immediately when the user presses Ctrl+C, potentially racing with or bypassing the TUI's normalonExitcleanup path (line 188-190).Consider removing the
SIGINThandler and relying on the TUI's existingonExitcallback for user-initiated exits. The other signal handlers (SIGHUP,SIGTERM,disconnect, stdin close) are appropriate for external/non-interactive termination scenarios.