-
Notifications
You must be signed in to change notification settings - Fork 2
ESD-1644-fix(output): stop spinner from racing os.Stdout swaps #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Phil-Browne
wants to merge
10
commits into
main
Choose a base branch
from
worktree-esd-1644-spinner-stdout-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3c857db
ESD-1644-fix(output): stop spinner from racing os.Stdout swaps
Phil-Browne 5b219c8
fix(output): move setStderr to output.go to fix wasm lint
Phil-Browne 32b8d0d
fix(output): synchronize spinner non-interactive stderr writes too
Phil-Browne 3182669
perf(output): avoid per-frame Sprintf allocation in spinner animation…
Phil-Browne cd032de
refactor(output): relocate stdErrStreamMu to common.go, quiet a race …
Phil-Browne 67ea5fa
perf(output): use writeSpinnerLinef for the remaining formatted spinn…
Phil-Browne e537500
fix(output): route interactive StopWithSuccess line through writeSpin…
Phil-Browne a6026fe
Merge remote-tracking branch 'origin/main' into worktree-esd-1644-spi…
Phil-Browne a7debc3
Merge remote-tracking branch 'origin/main' into worktree-esd-1644-spi…
Phil-Browne 1c1c71b
Merge branch 'main' into worktree-esd-1644-spinner-stdout-race
Phil-Browne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| //go:build !wasm | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestSpinnerRunWithPagerRace is a regression test for ESD-1644: a live | ||
| // spinner animating in the background must not race with RunWithPager | ||
| // concurrently swapping os.Stdout, and its frames (now routed to stderr) | ||
| // must never bleed into the pager's captured stdout content. Run with | ||
| // -race to exercise the regression this guards against. | ||
| func TestSpinnerRunWithPagerRace(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping timing-sensitive race regression test") | ||
| } | ||
| orig := isTerminalCached.Load() | ||
| t.Cleanup(func() { SetIsTerminal(orig) }) | ||
| SetIsTerminal(true) | ||
|
|
||
| // Tall terminal so RunWithPager always takes the direct-write path. | ||
| setTerminalHeightForTesting(1000) | ||
| t.Cleanup(func() { setTerminalHeightForTesting(0) }) | ||
|
|
||
| const iterations = 30 | ||
|
|
||
| stdout := captureStdout(t, func() { | ||
| captureStderr(t, func() { | ||
| spinner := NewSpinner(true) | ||
| spinner.Start("Racing...") | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < iterations; i++ { | ||
| wg.Add(1) | ||
| go func(i int) { | ||
| defer wg.Done() | ||
| _ = RunWithPager(func() error { | ||
| fmt.Printf("row%d\n", i) | ||
| return nil | ||
| }) | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
| spinner.Stop() | ||
| }) | ||
| }) | ||
|
|
||
| for i := 0; i < iterations; i++ { | ||
| assert.Contains(t, stdout, fmt.Sprintf("row%d", i)) | ||
| } | ||
| assert.NotContains(t, stdout, "\r\033[K", | ||
| "spinner frames must never bleed into the pager's stdout content") | ||
| } | ||
|
|
||
| // TestSpinnerCaptureOutputRace is a regression test for ESD-1644: a live | ||
| // spinner writing to os.Stderr must not race with CaptureOutput | ||
| // concurrently reassigning both os.Stdout and os.Stderr. Run with -race to | ||
| // exercise the regression this guards against. | ||
| func TestSpinnerCaptureOutputRace(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping timing-sensitive race regression test") | ||
| } | ||
| orig := isTerminalCached.Load() | ||
| t.Cleanup(func() { SetIsTerminal(orig) }) | ||
| SetIsTerminal(true) | ||
|
|
||
| captureStderr(t, func() { | ||
| spinner := NewSpinner(true) | ||
| spinner.Start("Racing...") | ||
|
|
||
| var wg sync.WaitGroup | ||
| const iterations = 30 | ||
| for i := 0; i < iterations; i++ { | ||
| wg.Add(1) | ||
| go func(i int) { | ||
| defer wg.Done() | ||
| CaptureOutput(func() { | ||
| fmt.Printf("captured%d\n", i) | ||
| }) | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
| spinner.Stop() | ||
| }) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.