fix(desktop): park terminal transports on detach — close the socket, resume by seq

Merged
KI
3mo ago
+261-1

Fixes SUPER-2043 (consolidates SUPER-1808, SUPER-2015 — same report from Clinton: multi-second UI hangs when typing in a Claude Code TUI or opening a new Terminal tab, only workspace navigation responsive, worsening since 1.17.0).

Mechanism

A parked (hidden) terminal pane kept its WebSocket open for the life of the session:

  1. Hidden output parsing. Every parked pane kept receiving and parsing the full output stream of its (often chatty, agent-driven) terminal — invisible renderer work that scales linearly with visited workspaces.
  2. Reconnect storms. On any endpoint failure (host-service restart, sleep/wake half-open sockets, port change), every parked transport re-dialed forever on 500ms→10s backoff. N parked panes × infinite retries inflate Chromium's per-renderer WebSocket handshake throttle, which then delays every new handshake by seconds.

Measured in the real app (CDP, this branch): a fresh handshake to host-service takes 2 ms; the same handshake right after a 50-dial burst takes 3,974 ms / 4,255 ms. Since #6299 terminal creation rides the WS handshake (create-on-attach), and a visible pane's reconnect does too — so an inflated throttle is exactly "new tab hangs for seconds" and "typing dead for seconds until re-attach" (keystrokes are dropped while the socket is down, terminal.onData gates on open).

The stale pty-daemon hypothesis in the original tickets is a real but separate bug (orphaned old-version daemon burning CPU, fix parked on fix/ptyd-orphaned-by-unlink-spawn); it runs in a separate process and cannot block the renderer.

Fix

detach() now parks the transport: tears down the liveness watchdog + resume listeners and closes the socket, keeping everything needed to resume (seq anchor, replay flags, title, logs, session-ended state). Remount's connect() dials a fresh socket carrying ?seq=<epoch>:<n>, so a seq-aware host replays exactly the bytes missed while parked (reanchor + repaint past the 2 MB ring). This is plan item 9 of plans/pty-lifecycle-cleanup.md ("park = disconnect"), now lossless thanks to the seq-catch-up work.

Parked panes cost nothing: no hidden parsing, no dials feeding the throttle, and the host stops broadcasting to them entirely.

Verification (CDP against the dev desktop app, real journey)

Before (park call disabled): enter workspace → run a 20s ticker → navigate away. Socket stays open (no webSocketClosed), 88 data frames delivered to the hidden pane in 45 s.

After: same journey. webSocketClosed fires the instant the pane parks; zero dials/frames while parked; on return the redial carries seq=887d7796818d3a3d:3143 and LATE-LINE-WHILE-PARKED (emitted while the socket was closed) appears exactly once — scrollback intact, no double-paint.

Throttle repro: in-renderer A/B above (2 ms → ~4 s) reproduces the user-visible hang and is the load the parked retry storms generate.

Tests

  • park closes the socket silently (no failure count, no logs, no diagnosis), keeps title + stream position
  • park stops the liveness watchdog; late socket events ignored
  • connect() after park dials a fresh socket anchored at the parked position; re-attach works
  • Full transport + registry suites pass (34 tests), desktop typecheck clean

Summary by cubic

Fixes SUPER-2043 — multi-second UI hangs when typing in a Claude Code TUI or opening a Terminal tab — by closing parked terminal sockets instead of keeping them alive. detach() now parks the transport: it tears down liveness/reconnect machinery and closes the socket, keeping the sequence anchor, title, logs, and session-ended state. Remount's connect() dials a fresh socket carrying the anchor, so a seq-aware host replays exactly the bytes missed while parked.

  • Parked terminals no longer parse hidden output or retry dials, which was inflating Chromium's WebSocket handshake throttle and delaying every new handshake by seconds.
  • Pre-seq hosts (bytes delivered, synced never seen) are not parked; they keep the always-connected behavior because they can't replay a parked gap.
  • Parking applies the close handler's anchor hygiene: an anchor advanced by uncounted bytes is dropped, while a valid counted anchor survives.
  • Parking resets per-connection flags so a redial that fails before attaching still counts toward the outage diagnosis.
  • A re-point to a new endpoint resets seq capability, so a legacy host on a new port isn't parked.
  • Adds tests for silent socket close, watchdog shutdown, anchored redial, pre-seq host exemption, poisoned-anchor drop, counted-anchor keep, endpoint re-point, and post-park failure accounting.

Summary by CodeRabbit

  • Bug Fixes
    • Hidden terminal panes now close their WebSocket connections, preventing unnecessary reconnection activity.
    • Terminal state, titles, and stream position are preserved while a pane is hidden.
    • Returning to a parked terminal automatically reconnects and resumes output from the correct position.
    • Improved handling of delayed connection events during pane transitions.
    • Connection failures after returning to a hidden terminal are now detected more reliably.
    • Improved recovery when connection sequence information is unavailable or outdated.

Revert plan (labeled revert-candidate)

If field regressions appear (missing scrollback after workspace switches, terminals stuck "connecting" on remount, double-painted output), revert first and diagnose after — this PR is built to revert cleanly:

  • Renderer-only; no protocol, host, or schema change. Old and new renderers speak the same terminal WS protocol, so a revert ships in any patch release with no ordering constraints.
  • No persisted-state migration: it writes the same snapshot + seq-anchor keys the pre-PR code already wrote, so profiles roll backward with no cleanup.
  • Two commits on one branch (522c1a5d5 park + tests, 5a78cf043 per-connection flag reset); git revert of the squash-merge commit restores exact pre-PR behavior (parked panes hold sockets again).
  • Watch after release: terminal_connect_failed telemetry and any report matching SUPER-2043's fingerprint; regressions specific to this PR would instead look like replay gaps or remount attach failures.