Skip to content

Fix: prevent crash on legacy X10 mouse reports after terminal rebuild - #6696

Open
Jyotish08 wants to merge 1 commit into
Textualize:mainfrom
Jyotish08:fix/issue-3
Open

Fix: prevent crash on legacy X10 mouse reports after terminal rebuild#6696
Jyotish08 wants to merge 1 commit into
Textualize:mainfrom
Jyotish08:fix/issue-3

Conversation

@Jyotish08

@Jyotish08 Jyotish08 commented Aug 9, 2026

Copy link
Copy Markdown

closes #6668 ### Problem
When a terminal emulator is torn down and rebuilt underneath a running Textual application (e.g. VS Code terminal reload, tmux detach/reattach, SSH reconnect):

  1. The rebuilt terminal resets its negotiation and falls back to legacy X10 mouse reporting (ESC [ M Cb Cx Cy).
  2. When the pointer moves past column ~95 (Cx > 127), the raw coordinate bytes exceed 0x7F (non-ASCII).
  3. The input thread's strict UTF-8 incremental decoder raises UnicodeDecodeError, causing the input loop and application to crash.
  4. If the crash is avoided, mouse tracking remains broken because the terminal stays in legacy X10 encoding instead of SGR mode (?1006).

Solution

  1. Fault-Tolerant Input Decoding: Updated incremental UTF-8 decoders in linux_driver.py, linux_inline_driver.py, and web_driver.py to use errors="replace". This prevents UnicodeDecodeError when raw non-ASCII X10 mouse bytes (> 0x7F) are read from stdin.
  2. Automatic SGR Mouse Mode Re-assertion:
    • Added LegacyMouseReport message class in messages.py.
    • Updated XTermParser in _xterm_parser.py to emit LegacyMouseReport upon encountering an X10 mouse sequence (ESC [ M ...).
    • Updated Driver.process_message to handle LegacyMouseReport by invoking self._enable_mouse_support(), re-asserting SGR mouse mode (\x1b[?1006h) to restore full mouse tracking.
  3. Tests:
    • Added test_legacy_x10_mouse_report_emits_legacy_mouse_report in tests/test_xterm_parser.py.
    • Added test_driver_reasserts_mouse_on_legacy_report in tests/test_driver.py.

1. Decode stdin with errors='replace' in input loops to prevent UnicodeDecodeError on raw non-ASCII X10 mouse coordinate bytes (> 0x7F).

2. Emit LegacyMouseReport from XTermParser on ESC [ M sequence and re-assert SGR mouse mode in driver to restore SGR mouse encoding after terminal teardown/rebuild.
Copilot AI lite review requested due to automatic review settings August 9, 2026 10:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens Textual’s input pipeline against legacy X10 mouse reporting that can appear after a terminal teardown/rebuild (e.g., VS Code terminal reload), preventing UTF-8 decode crashes and restoring full mouse tracking by re-enabling SGR mouse mode when X10 sequences are detected.

Changes:

  • Switch stdin byte-stream decoding in terminal/web drivers to a fault-tolerant UTF-8 incremental decoder (errors="replace") to avoid UnicodeDecodeError on non-UTF-8 X10 coordinate bytes.
  • Introduce a LegacyMouseReport message and teach XTermParser to emit it when ESC [ M ... (X10) is seen, prompting the driver to re-assert mouse mode via _enable_mouse_support().
  • Add targeted tests covering parser emission and driver behavior on legacy X10 reports.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_xterm_parser.py Adds regression test ensuring X10 sequences emit LegacyMouseReport.
tests/test_driver.py Adds regression test ensuring the driver re-enables mouse support on LegacyMouseReport.
src/textual/messages.py Introduces the LegacyMouseReport message type.
src/textual/drivers/web_driver.py Makes UTF-8 decoding tolerant (errors="replace") in the web input loop.
src/textual/drivers/linux_inline_driver.py Makes UTF-8 decoding tolerant (errors="replace") in the inline terminal input loop.
src/textual/drivers/linux_driver.py Makes UTF-8 decoding tolerant (errors="replace") in the terminal input loop.
src/textual/driver.py Handles LegacyMouseReport by re-enabling mouse support from the input thread.
src/textual/_xterm_parser.py Detects X10 mouse sequences and emits LegacyMouseReport prior to mouse parsing.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

UnicodeDecodeError crash: terminal rebuild (tmux reattach / VS Code reload) drops SGR mouse negotiation, falls back to X10 raw bytes

2 participants