Skip to content

Commit 306c518

Browse files
authored
fix(pb): make disableProgress race-free with sync/atomic.Bool (#511)
internal/pb package used an unprotected package-level `disableProgress` bool that's read by `(*ProgressBar).Add` from the concurrent pull/push worker goroutines while `SetDisableProgress` flips it on startup. `go test -race` reports the expected data race, and the Go memory model leaves the behaviour undefined in practice. Swap the bool for an atomic.Bool. Store from SetDisableProgress, Load from Add. No public-API change - callers continue to pass plain bool to SetDisableProgress. Fixes #493 Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com> Co-authored-by: SAY-5 <SAY-5@users.noreply.github.com>
1 parent f0ef94b commit 306c518

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

internal/pb/pb.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"os"
2323
"sync"
24+
"sync/atomic"
2425
"time"
2526

2627
humanize "github.com/dustin/go-humanize"
@@ -30,13 +31,16 @@ import (
3031
)
3132

3233
var (
33-
// disableProgress is the flag to disable progress bar.
34-
disableProgress bool
34+
// disableProgress is the flag to disable progress bar. Read from the
35+
// progress-bar hot path by concurrent pull/push workers while a single
36+
// goroutine may flip it via SetDisableProgress, so the access is guarded
37+
// by sync/atomic to stay race-free under `go test -race`.
38+
disableProgress atomic.Bool
3539
)
3640

3741
// SetDisableProgress disables the progress bar.
3842
func SetDisableProgress(disable bool) {
39-
disableProgress = disable
43+
disableProgress.Store(disable)
4044
}
4145

4246
// NormalizePrompt normalizes the prompt string.
@@ -85,7 +89,7 @@ func NewProgressBar(writers ...io.Writer) *ProgressBar {
8589
// Add adds a new progress bar.
8690
func (p *ProgressBar) Add(prompt, name string, size int64, reader io.Reader) io.Reader {
8791
// Return the reader directly if progress is disabled.
88-
if disableProgress {
92+
if disableProgress.Load() {
8993
return reader
9094
}
9195

0 commit comments

Comments
 (0)