Repository navigation
fix(agentop): end the TUI on Windows when stdin is redirected - #2178
michaljacovi wants to merge 1 commit into
Conversation
The hang in rossoctl#2158 is not test-only. When stdin is not a terminal, bubbletea opens the console (CONIN$) itself and closes it when the program ends, and on Windows that Close waits for the read loop's console read, which nothing interrupts. So agentop started from a console with stdin redirected or piped (agentop < NUL, a script piping into it) restored the screen after q or Ctrl+C, then stayed alive until a key was pressed, and swallowed that key. The test hung the same way. On Windows, when stdin is not a terminal, tui.Run now opens CONIN$ itself and passes it with tea.WithInput: bubbletea reads it and does not close it, and the process exits after Run anyway. With no console the open fails and nothing changes. Other platforms are unchanged. TestRun_StdinRedirectedEndsWithItsContext (Windows, stdin redirected): Run ends 1.5s after start, when its context does; without the fix it does not return. The endpoint-bypass test now also checks that Run was ended by its context. Fixes rossoctl#2158 Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Michal Jacovi <michal.jacovi@il.ibm.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The fix is right. With the default input, bubbletea v1.3.10 opens CONIN$ itself and runs defer f.Close() on it (tea.go, the defaultInput case). With tea.WithInput it takes the customInput path and never closes the file. On Windows, cancelreader wraps a non-stdin file in fallbackCancelReader, whose Close does nothing, so Run returns without waiting for the blocked console read. The only caller, cmd/agentop/main.go, returns right after tui.Run, so the file that is never closed and the parked read goroutine are harmless. Other platforms are unchanged.
One thing blocks: Pre-commit Checks is red because of this PR's own tests. check-test-timing flags 3 lines (console_windows_test.go:30, :38 and picker_test.go:346). I ran python scripts/dev/check-test-timing.py locally on the head and got the same 3 findings. CONTRIBUTING.md "Tests and time" says "A hang is the failure. No time.After guard beside the wait", so the 20s guards need to go. The 1.5s AfterFunc needs a // timing: <reason> mark. Details are inline.
One suggestion: the assertion errors.Is(err, tea.ErrProgramKilled) also passes when bubbletea recovers a panic in the model, which is the nil-dereference the bypass test is there to catch. Asserting context.Canceled is precise.
FYI: the Windows CI job vets ./tui but runs only a fixed list of tests from package ., so neither changed test runs on Windows in CI. The new test would skip there anyway, because it needs a console. The Windows evidence rests on the manual runs in the description, which is fine for this change.
Author: michaljacovi (maintainer)
Areas reviewed: Go (cmd/agentop/tui), tests, CI
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: failing (Pre-commit Checks: check-test-timing); all other completed jobs pass; Go CI (README demo) still pending
| if !errors.Is(err, tea.ErrProgramKilled) { | ||
| t.Errorf("Run = %v, want it ended by its context", err) | ||
| } | ||
| case <-time.After(20 * time.Second): |
There was a problem hiding this comment.
must-fix: check-test-timing fails here ("a time limit beside a wait"), so Pre-commit Checks is red. CONTRIBUTING.md "Tests and time" says a hang is the failure: go test -timeout ends it and dumps every goroutine's stack, which says more than this t.Fatal. Drop the goroutine and the select and call it directly:
err := Run(ctx, RunOptions{Endpoint: "http://127.0.0.1:1"})
if !errors.Is(err, context.Canceled) {
t.Errorf("Run = %v, want it ended by its context", err)
}| } | ||
| _ = f.Close() | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| time.AfterFunc(1500*time.Millisecond, cancel) |
There was a problem hiding this comment.
must-fix: also flagged by check-test-timing ("sets a real timeout"). Here the delay is load-bearing. With a context that is already cancelled, the hang only showed up about 1 in 5 times, because the read loop has to be blocked in the console read before Run shuts down. Nothing in Run signals that moment. The hook accepts a reasoned exemption for this case, e.g.
// timing: the hang needs bubbletea's read loop blocked in a console read before ctx ends,
// and nothing signals that it is.
time.AfterFunc(1500*time.Millisecond, cancel)| if !errors.Is(err, tea.ErrProgramKilled) { | ||
| t.Errorf("Run = %v, want it ended by its context", err) | ||
| } | ||
| case <-time.After(20 * time.Second): |
There was a problem hiding this comment.
must-fix: same check-test-timing failure as in console_windows_test.go:38. With input: strings.NewReader("") and a context that is already cancelled, Run cannot block on input anymore. A plain err := Run(ctx, opts) followed by the assertion is enough, and a regression would still show up as a go test -timeout hang with stacks.
| go func() { done <- Run(ctx, opts) }() | ||
| select { | ||
| case err := <-done: | ||
| if !errors.Is(err, tea.ErrProgramKilled) { |
There was a problem hiding this comment.
suggestion: tea.ErrProgramKilled is too broad for this test. When bubbletea recovers a panic in the model, it returns fmt.Errorf("%w: %w", ErrProgramKilled, ErrProgramPanic) (tea.go:637). So a nil dereference of Lister/PortForwarder, which is what this test guards against, would still pass. On a context cancel it returns fmt.Errorf("%w: %w", ErrProgramKilled, p.externalCtx.Err()) (tea.go:727), so errors.Is(err, context.Canceled) matches only "ended by its context". The same applies at console_windows_test.go:35.
Fixes #2158.
TestRunOptionsWiringEndpointBypasssometimes hung on Windows untilgo testtimed out. The same hang reaches users.Why: when stdin is not a terminal, bubbletea opens the console (
CONIN$) itself and closes it when the program ends. On Windows thatClosewaits for the read loop's console read, which nothing interrupts. So agentop started from a console with stdin redirected or piped (agentop < NUL, or a script piping into it) restored the screen afterqor Ctrl+C, then stayed alive until a key was pressed, and swallowed that key.The change:
tui.RunopensCONIN$itself and passes it withtea.WithInput. bubbletea reads it and does not close it, and the process exits afterRunanyway. With no console the open fails and nothing changes. Other platforms are unchanged.RunOptionsgets an unexportedinput, for tests only. The endpoint-bypass test passes an empty reader, fails with a message rather than a timeout ifRundoes not return, and checks thatRunwas ended by its context.Tested on Windows, with stdin redirected from a console:
TestRun_StdinRedirectedEndsWithItsContext:Runends when its context does, 1.5s after start. Without the fix it does not return.go vetpasses for linux, darwin and windows.Assisted-By: Claude (Anthropic AI) noreply@anthropic.com