Skip to content

Commit 2e53f3f

Browse files
agutmanstein-scaleclaude
authored andcommitted
fix(tui): handle external SIGTSTP to prevent mouse garbling on suspend
When opencode receives SIGTSTP from an external source (code-server terminal management, job control, kill -TSTP), the process is suspended without terminal cleanup. Mouse tracking remains enabled, causing mouse events to echo as garbled escape sequences in the shell. Register SIGTSTP/SIGCONT handlers after renderer creation that call renderer.suspend()/resume() — the same cleanup the manual suspend keybind uses. The SIGTSTP handler removes itself before re-raising to allow default suspend behavior. Includes test-sigtstp-mouse.sh reproduction script. Upstream fix: anomalyco/opentui#907 Closes anomalyco#20506 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 929adfa commit 2e53f3f

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ export function tui(input: {
192192

193193
const renderer = await createCliRenderer(rendererConfig(input.config))
194194

195+
// Handle external SIGTSTP (e.g., code-server terminal management, job control)
196+
// to properly disable mouse tracking before suspension. Without this, the shell
197+
// receives mouse events as garbled escape sequences.
198+
const sigtstpHandler = () => {
199+
renderer.suspend()
200+
process.removeListener("SIGTSTP", sigtstpHandler)
201+
process.kill(process.pid, "SIGTSTP")
202+
}
203+
const sigcontHandler = () => {
204+
process.on("SIGTSTP", sigtstpHandler)
205+
renderer.resume()
206+
}
207+
process.on("SIGTSTP", sigtstpHandler)
208+
process.on("SIGCONT", sigcontHandler)
209+
195210
await render(() => {
196211
return (
197212
<ErrorBoundary
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Reproduction script for SIGTSTP mouse garbling.
4+
#
5+
# Usage:
6+
# 1. In Terminal 1: Run opencode normally
7+
# 2. In Terminal 2: Run this script with the opencode PID:
8+
# ./test-sigtstp-mouse.sh <PID>
9+
# 3. In Terminal 1: Move the mouse after opencode is suspended
10+
#
11+
# Without the fix: garbled escape sequences appear (35;89;19M35;84;20M...)
12+
# With the fix: clean shell prompt, no garbled text
13+
#
14+
# You can also test without opencode by using any TUI that enables mouse:
15+
# python3 -c "import sys; sys.stdout.write('\x1b[?1003h\x1b[?1006h'); input()"
16+
# Then send SIGTSTP from another terminal.
17+
18+
set -e
19+
20+
if [ -z "$1" ]; then
21+
echo "Usage: $0 <PID>"
22+
echo ""
23+
echo "Sends SIGTSTP to the given PID to test mouse cleanup."
24+
echo "Run opencode in another terminal first, then pass its PID."
25+
echo ""
26+
echo "Find the PID with: pgrep -f opencode"
27+
exit 1
28+
fi
29+
30+
PID="$1"
31+
32+
echo "Sending SIGTSTP to PID $PID..."
33+
echo "Switch to the opencode terminal and move the mouse."
34+
echo "If you see garbled text like '35;89;19M35;84;20M...' the bug is present."
35+
echo ""
36+
37+
kill -TSTP "$PID"
38+
39+
echo "Done. Resume with: kill -CONT $PID (or 'fg' in the opencode terminal)"

0 commit comments

Comments
 (0)