Skip to content

fix(agentop): end the TUI on Windows when stdin is redirected - #2178

Open
michaljacovi wants to merge 1 commit into
rossoctl:mainfrom
michaljacovi:tui-test-stdin-hang
Open

michaljacovi wants to merge 1 commit into
rossoctl:mainfrom
michaljacovi:tui-test-stdin-hang

Conversation

@michaljacovi

Copy link
Copy Markdown
Contributor

Fixes #2158. TestRunOptionsWiringEndpointBypass sometimes hung on Windows until go test timed 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 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, or 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 change:

  • On Windows, when stdin is not a terminal, tui.Run 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.
  • RunOptions gets an unexported input, for tests only. The endpoint-bypass test passes an empty reader, fails with a message rather than a timeout if Run does not return, and checks that Run was ended by its context.

Tested on Windows, with stdin redirected from a console:

  • TestRun_StdinRedirectedEndsWithItsContext: Run ends when its context does, 1.5s after start. Without the fix it does not return.
  • The endpoint-bypass test passes 20 of 20 runs. Before, about 1 in 5 hung.
  • go vet passes for linux, darwin and windows.

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

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>
@michaljacovi
michaljacovi requested a review from a team as a code owner November 29, 2026 19:10
@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 498d5cb8-8223-454e-b1e3-a77692fd52f6
Commits

Reviewing files that changed from the base of the PR and between a7a4315 and a616be2.

Files selected for processing: 5

  • cmd/agentop/tui/app.go
  • cmd/agentop/tui/console_other.go
  • cmd/agentop/tui/console_windows.go
  • cmd/agentop/tui/console_windows_test.go
  • cmd/agentop/tui/picker_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 29, 2026

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

Status: New/ToDo

Development

Successfully merging this pull request may close these issues.

agentop tui: TestRunOptionsWiringEndpointBypass hangs intermittently on Windows

3 participants