Skip to content

fix(wt menu): share one stdin reader across sequential menus - #11

Merged
sahil87 merged 1 commit into
mainfrom
fix-menu-session-byte-theft
May 31, 2026
Merged

sahil87 merged 1 commit into
mainfrom
fix-menu-session-byte-theft

Conversation

@sahil87

@sahil87 sahil87 commented May 31, 2026

Copy link
Copy Markdown
Owner

Problem

In wt open, pressing Enter on the second menu ("Open in:") did nothing — you had to arrow up/down first, then Enter worked. Reported against the arrow-key menu shipped in #10.

Root cause

The arrow-key menu reads stdin through a pump goroutine that reads one byte ahead (its result channel is buffered with cap 1). Each ShowMenu call built its own reader over os.Stdin. After a menu submitted on Enter, its pump was left parked inside a blocking ReadByte() on the shared fd — an orphan nobody cancelled. When the next menu in the same flow started, its fresh reader raced that orphan; the orphan had been parked first, so it stole the next menu's first keystroke. An arrow key is a multi-byte burst (\x1b[B), so enough bytes eventually spilled into the second reader for it to respond — hence "arrow first, then Enter works."

wt delete had the same latent bug in its chained menus (selection → uncommitted-changes → unpushed-commits → confirm).

Fix

Introduce MenuSession, which owns a single shared stdin reader reused across every Show() call — so there is never a second reader to race, and no keystroke is stolen between menus.

  • Raw mode is entered/restored per Show() (not held across the session), so the terminal is always in cooked mode between menus. This keeps the delete flow's os.Exit cancel paths safe with no special-casing — the byte-theft fix only needs a shared reader, not continuously-held raw mode.
  • ShowMenu is now a thin one-shot session wrapper, so single-menu callers (create) are unchanged.
  • wt open and wt delete create one session and thread it through their chained menus.

Tests

  • TestMenuSession_SharesReaderAcrossMenus — two menus (the wt open shape) through one session; asserts the second submits on Enter.
  • TestMenuSession_ThreeMenusNoTheft — three menus (the wt delete worst case) through one session.
  • TestUnderlyingReadAhead_DemonstratesTheft — characterization test documenting the read-ahead theft the fix routes around.

go build, go vet, and the full internal/worktree + cmd/wt suites pass. (TestCreate_WorktreeOpenDefault fails on this branch's base too — it's a pre-existing, environment-dependent failure: no default app is installed in the test env, so the launch-guard marker is never emitted. Unrelated to this change.)

🤖 Generated with Claude Code

The arrow-key menu reads stdin through a pump goroutine that reads one
byte ahead (its result channel is buffered with cap 1). Each ShowMenu
built its own reader over os.Stdin, so after a menu submitted on Enter
its pump was left parked inside a blocking ReadByte() on the shared fd —
an orphan nobody cancelled. When the next menu in the same flow started,
its fresh reader raced that orphan, which had been parked first and
stole the next menu's first keystroke.

Symptom: in `wt open`, pressing Enter on the second screen ("Open in:")
did nothing until an arrow key (a multi-byte burst) spilled enough bytes
for the second reader to react. `wt delete` had the same latent bug in
its chained menus (selection → uncommitted → unpushed → confirm).

Fix: introduce MenuSession, which owns a single shared stdin reader
reused across every Show() call, so there is never a second reader to
race. Raw mode is entered/restored per Show() (not held across the
session) so the terminal is always in cooked mode between menus — the
delete flow's os.Exit cancel paths stay safe with no special-casing.
ShowMenu is now a one-shot session wrapper, so single-menu callers
(create) are unchanged. `wt open` and `wt delete` thread one session
through their chained menus.

Regression tests drive two- and three-menu sequences through one
session over a shared stream and assert each menu receives its
keystroke; a characterization test documents the underlying read-ahead
theft the fix routes around.
@sahil87
sahil87 merged commit a631c9d into main May 31, 2026
@sahil87
sahil87 deleted the fix-menu-session-byte-theft branch May 31, 2026 02:18
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.

1 participant