Skip to content

fix(tui): restore terminal state on crash and release leaked resources - #1272

Merged
liruifengv merged 3 commits into
mainfrom
fix/tui-resource-leaks
Jul 1, 2026
Merged

fix(tui): restore terminal state on crash and release leaked resources#1272
liruifengv merged 3 commits into
mainfrom
fix/tui-resource-leaks

Conversation

@liruifengv

Copy link
Copy Markdown
Collaborator

Related Issue

No prior issue — this addresses resource leaks and terminal-state corruption found during an internal audit of the TUI.

Problem

The TUI had several resource-management gaps:

  • After an uncaught exception, unhandled rejection, or a SIGTERM whose cleanup threw, the terminal was left in raw mode with a hidden cursor, bracketed paste still on, and XON/XOFF flow control disabled — the user's shell looked broken afterwards.
  • stty -ixon was set on startup but never restored, so flow control stayed disabled in the user's shell after exit.
  • Pasted image bytes were held for the whole session and never released when their transcript entries were trimmed, so memory grew in long, screenshot-heavy sessions.
  • Several timers (feedback spinner, activity spinner, transcript components, footer, editor double-Esc) were not stopped during shutdown, leaking intervals on in-process restart.

What changed

  • Add a best-effort terminal-restore helper and call it from the uncaughtException / unhandledRejection handlers and the emergency exit path.
  • Wrap shutdown so the terminal is restored even when session or harness teardown throws.
  • Save the original stty settings on startup and restore them on exit.
  • Reclaim pasted image bytes once their owning transcript entries are trimmed.
  • Stop feedback, activity, transcript, footer, and editor timers during shutdown; make reverse-RPC panel cleanup idempotent and event-loop re-entry safe.
  • Add unit coverage for the new image-store removal API.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Restore raw mode, cursor, and flow control on uncaughtException, unhandledRejection, and SIGTERM cleanup failure; reclaim pasted image bytes when transcript entries are trimmed; and stop feedback, activity, transcript, footer, and editor timers during shutdown.
@changeset-bot

changeset-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 98b6978

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 1, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@98b6978
npx https://pkg.pr.new/@moonshot-ai/kimi-code@98b6978

commit: 98b6978

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 52ae40dd73

ℹ️ 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".

Comment thread apps/kimi-code/src/cli/run-shell.ts Outdated
Comment thread apps/kimi-code/src/cli/run-shell.ts Outdated
Keep uncaughtException / unhandledRejection handlers installed for the whole interactive session; removing them right after start() resolved left runtime crashes uncaught. Run stty with stdin inherited from the TTY, since stty fails when stdin is /dev/null.
@liruifengv
liruifengv merged commit 54703d9 into main Jul 1, 2026
9 checks passed
@liruifengv
liruifengv deleted the fix/tui-resource-leaks branch July 1, 2026 13:14
@github-actions github-actions Bot mentioned this pull request Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant