Skip to content

fix(tui): reset terminal state on crash/exit (Windows) - #21278

Closed
jeankrlo2929 wants to merge 1 commit into
anomalyco:devfrom
jeankrlo2929:fix/windows-terminal-reset-on-crash
Closed

jeankrlo2929 wants to merge 1 commit into
anomalyco:devfrom
jeankrlo2929:fix/windows-terminal-reset-on-crash

Conversation

@jeankrlo2929

Copy link
Copy Markdown

Problem

On Windows (PowerShell), when OpenCode exits unexpectedly or crashes, the terminal is left in a broken state. Raw ANSI escape sequences appear as literal text at the bottom of the screen (e.g. 555;59;86m) and the cursor may be hidden, requiring the user to close and reopen PowerShell.

Reported in #21277.

Root cause

The finally blocks in thread.ts and attach.ts call unguard?.() which restores the Windows console mode, but they never send ANSI reset sequences. If the renderer (@opentui/solid) crashes before it can clean up, the alternate screen buffer and all ANSI attributes remain active.

Fix

Added a resetTerminal() function in both thread.ts and attach.ts that sends:

Sequence Effect
\x1b[0m Reset all attributes (colors, bold, italic, etc.)
\x1b[?25h Restore cursor visibility
\x1b[?1049l Exit alternate screen buffer
\x1b[?7h Re-enable auto-wrap mode

In thread.ts, also registered a process.on('exit') handler as a backstop that fires even on process.exit() or uncaught exceptions, before the finally block has a chance to run.

Testing

Reproduced on Windows 10 / PowerShell 5.1 / OpenCode 1.3.17 by forcing a crash. After this fix, the terminal restores cleanly on exit.

On Windows, when OpenCode exits unexpectedly or crashes, the terminal
is left in a broken state showing raw ANSI escape sequences as literal
text (e.g. "555;59;86m") and the cursor may be hidden.

Add resetTerminal() in thread.ts and attach.ts that:
- Sends \x1b[0m  to reset all ANSI attributes (colors, bold, etc.)
- Sends \x1b[?25h to restore the cursor
- Sends \x1b[?1049l to exit the alternate screen buffer
- Sends \x1b[?7h to re-enable auto-wrap

Also registers a process.on("exit") handler in thread.ts as a
backstop that runs even on process.exit() or uncaught exceptions.

Fixes: anomalyco#21277
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 7, 2026
@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 7, 2026
@github-actions github-actions Bot closed this Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant