Repository navigation
fix: show shutdown progress and force exit on Ctrl+C during shutdown hooks - #4164
Conversation
🦋 Changeset detectedLatest commit: 8c5acad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 264cb6cd25
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // A Ctrl+C during shutdown means "don't wait for slow hooks": skip the | ||
| // remaining graceful close and get out immediately. | ||
| this.state.editor.onCtrlC = () => { | ||
| this.emergencyTerminalExit(130); |
There was a problem hiding this comment.
Leave fullscreen before forcing the process to exit
When tui_mode = "fullscreen" and the user invokes the new Ctrl+C escape while closeSession() is waiting, this call goes directly to process.exit() through emergencyTerminalExit() and bypasses stopUiForExit(). restoreTerminalModes() does not emit the alternate-screen/mouse/autowrap teardown; those sequences (ESC[?1049l, mouse disable, and autowrap enable) are only sent by TuiAltScreen.stop() in packages/pi-tui/src/tui-alt-screen.ts:392-418, so the user's shell remains in Kimi's alternate screen with mouse reporting active. On POSIX the same bypass also skips run-shell.ts's saved-stty restoration. Perform complete UI/terminal cleanup before the forced exit.
AGENTS.md reference: AGENTS.md:L114-L116
Useful? React with 👍 / 👎.
| this.aborted = true; | ||
| // A Ctrl+C during shutdown means "don't wait for slow hooks": skip the | ||
| // remaining graceful close and get out immediately. | ||
| this.state.editor.onCtrlC = () => { |
There was a problem hiding this comment.
Capture shutdown Ctrl+C outside the editor
When shutdown starts while an editor replacement is focused—for example, SIGTERM arrives with /help or a settings picker open and a SessionEnd hook then blocks—pi-tui routes Ctrl+C only to the focused component (packages/pi-tui/src/tui.ts:1080-1090). stop() neither restores editor focus nor adds a global input listener, so changing state.editor.onCtrlC is unreachable; HelpPanel and ChoicePicker ignore Ctrl+C and the promised immediate exit still waits for the hook timeout. Capture the shutdown key at the UI level or otherwise independently of focus.
AGENTS.md reference: apps/kimi-code/AGENTS.md:L25-L26
Useful? React with 👍 / 👎.
| }; | ||
|
|
||
| editor.onPreInput = (data: string) => { | ||
| if (this.disposed) return false; |
There was a problem hiding this comment.
Quiesce every editor shortcut after disposal
After editorKeyboard.dispose() during a slow closeSession(), returning false here tells CustomEditor.handleInput() to continue dispatching the key. Several installed handlers still lack a disposed check—notably Ctrl+G's onOpenExternalEditor, plus Shift-Tab, Ctrl+S/B/O, paste, and history—so Ctrl+G can call state.ui.stop(), spawn $EDITOR, and later restart the UI concurrently with shutdown instead of leaving only emergency Ctrl+C active. Consume non-Ctrl+C input after disposal or clear/guard every remaining handler.
Useful? React with 👍 / 👎.
…rminal on force exit
|
Review feedback addressed in
Tests updated accordingly; full suite and e2e (15s slow-hook scenario, force exit within seconds) verified. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8c5acadd2c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // input-listener level so it works no matter which component holds focus | ||
| // (a dialog may have replaced the editor). | ||
| const removeForceExitListener = this.state.ui.addInputListener((data) => { | ||
| if (!matchesKey(data, Key.ctrl('c'))) return undefined; |
There was a problem hiding this comment.
Accept only Ctrl+C press events in the shutdown listener
When a normal double-Ctrl+C exit is waiting in closeSession() on a terminal that supports Kitty event reporting, releasing the second Ctrl+C also matches this condition and immediately forces the exit without a third key press. ProcessTerminal requests Kitty flag 2 (packages/pi-tui/src/terminal.ts:251-255), matchesKey() matches the resulting \x1b[99;5:3u release sequence, and the generic release filter in packages/pi-tui/src/tui.ts:1024-1087 runs only after global input listeners, so slow SessionEnd hooks and harness cleanup are silently skipped. Reject release/repeat events here and act only on a new press.
AGENTS.md reference: AGENTS.md:L118-L120
Useful? React with 👍 / 👎.
| if (this.onExit) { | ||
| void this.onExit(130); |
There was a problem hiding this comment.
Bypass foreground handoffs during a forced exit
When shutdown was initiated by /web or /remote-control, those commands register exitForegroundTask before calling stop() (apps/kimi-code/src/tui/commands/web.ts:41-42,55-110); routing forced Ctrl+C through the normal onExit therefore reaches apps/kimi-code/src/cli/run-shell.ts:258-263, which awaits that foreground task instead of calling process.exit. In this context the advertised forced exit starts and remains in the web/remote-control server (and the still-running original shutdown can later invoke the handoff again), so the force path must clear or bypass the takeover.
AGENTS.md reference: AGENTS.md:L114-L116
Useful? React with 👍 / 👎.
| // stty restore via onExit, instead of the bare emergency sequences. | ||
| private forceExitDuringShutdown(): void { | ||
| try { | ||
| this.stopUiForExit(); |
There was a problem hiding this comment.
Drain pending input before the forced terminal teardown
When /exit, Ctrl+D, or SIGTERM has already started shutdown and the user force-exits with Ctrl+C on a Kitty terminal over slow SSH, the release sequence can still be in flight when this call removes and pauses stdin. The normal path explicitly awaits state.terminal.drainInput() at kimi-tui.ts:1076-1082, and ProcessTerminal.drainInput() is documented and implemented specifically to consume late Kitty releases before returning control to the parent shell (packages/pi-tui/src/terminal.ts:78-84,384-419). The new stopUiForExit() call fixes screen teardown but still bypasses that drain, so the release escape sequence can leak into the user's shell; perform the short drain before stopping the UI.
AGENTS.md reference: AGENTS.md:L114-L116
Useful? React with 👍 / 👎.
Requirement or Bug
退出 Kimi Code 时,如果配置了耗时较长的 SessionEnd hook(hook
timeout最长 30 秒),界面会冻结在最后一帧且无任何提示,看起来像卡死;期间再按 Ctrl+C 也没有反应。Bug Reproduction Steps
config.toml配置一个耗时的 SessionEnd hook(例如command = "sleep 15", timeout = 30)。kimi,发送任意消息(如! echo hi)创建会话。Ctrl-C退出:第二次按下后界面冻结、无任何提示,15 秒后(hook 结束)才退出;冻结期间再按Ctrl-C无任何反应。Root Cause
KimiTUI.stop()中closeSession('shutting down')会同步执行 SessionEnd hooks,时长由用户 hook 配置决定(最长 30 秒),期间 TUI 停在最后一帧,用户无法区分"正在关闭"和"程序卡死"。Ctrl-C会走正常的取消/退出确认逻辑,把关闭提示覆盖掉(clearPendingExit/armPendingExit),或者干脆被忽略,没有任何"立即退出"的通道。这是根本修复:关闭期间给出可见反馈,并把该窗口内的
Ctrl-C定义为"跳过等待、立即强制退出"。Code Changes
说明:
Ctrl-C在 raw 模式下以字节\x03到达,stop()内直接替换editor.onCtrlC即可接管;emergencyTerminalExit会恢复终端模式(raw mode / 光标)后以 130(128+SIGINT)退出。Behavior Changes and Affected Users
Shutting down… (running shutdown hooks)Ctrl-CCtrl-C无效果的 TUI 用户Esc/ 输入 / 其他快捷键clearPendingExit)Ctrl-C/Ctrl-D退出、任务运行中Ctrl-C取消受影响模块与测试覆盖:
editor-keyboard.ts(dispose 后失效)→test/tui/controllers/editor-keyboard.test.ts新增 1 个用例:dispose 后Ctrl-C/Esc/submit 不触碰 footer 提示、不触发stop。kimi-tui.ts(stop() 提示 + 强制退出接管)→test/tui/kimi-tui-message-flow.test.ts新增 1 个用例:stop()挂起在closeSession时按Ctrl-C会调用emergencyTerminalExit(130)。Ctrl-C进程在 2 秒内退出(不等待 hook)。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.