fix(tui): reset terminal state on crash/exit (Windows) - #21278
Closed
jeankrlo2929 wants to merge 1 commit into
Closed
jeankrlo2929 wants to merge 1 commit into
jeankrlo2929 wants to merge 1 commit into
Conversation
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
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
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. |
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
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. |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
finallyblocks inthread.tsandattach.tscallunguard?.()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 boththread.tsandattach.tsthat sends:\x1b[0m\x1b[?25h\x1b[?1049l\x1b[?7hIn
thread.ts, also registered aprocess.on('exit')handler as a backstop that fires even onprocess.exit()or uncaught exceptions, before thefinallyblock 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.