Repository navigation
fix(wt menu): share one stdin reader across sequential menus - #11
Merged
Merged
Conversation
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.
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
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
ShowMenucall built its own reader overos.Stdin. After a menu submitted on Enter, its pump was left parked inside a blockingReadByte()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 deletehad 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 everyShow()call — so there is never a second reader to race, and no keystroke is stolen between menus.Show()(not held across the session), so the terminal is always in cooked mode between menus. This keeps the delete flow'sos.Exitcancel paths safe with no special-casing — the byte-theft fix only needs a shared reader, not continuously-held raw mode.ShowMenuis now a thin one-shot session wrapper, so single-menu callers (create) are unchanged.wt openandwt deletecreate one session and thread it through their chained menus.Tests
TestMenuSession_SharesReaderAcrossMenus— two menus (thewt openshape) through one session; asserts the second submits on Enter.TestMenuSession_ThreeMenusNoTheft— three menus (thewt deleteworst case) through one session.TestUnderlyingReadAhead_DemonstratesTheft— characterization test documenting the read-ahead theft the fix routes around.go build,go vet, and the fullinternal/worktree+cmd/wtsuites pass. (TestCreate_WorktreeOpenDefaultfails 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