Skip to content

Commit 63df4aa

Browse files
authored
Merge pull request #202 from onflow/holyfuchs/fix-spinner-race
Fix spinner race condition
2 parents c20b81f + 019f688 commit 63df4aa

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

output/spinner.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ package output
2020

2121
import (
2222
"fmt"
23+
"sync"
2324
"time"
2425

2526
"github.com/gosuri/uilive"
2627
)
2728

2829
var spinnerCharset = []rune{'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'}
30+
var mu = sync.Mutex{}
2931

3032
type Spinner struct {
3133
prefix string
@@ -46,7 +48,10 @@ func (s *Spinner) Start() {
4648
}
4749

4850
func (s *Spinner) run() {
51+
mu.Lock()
52+
// writes to global variable, need a global lock to avoid race conditions
4953
writer := uilive.New()
54+
mu.Unlock()
5055

5156
ticker := time.NewTicker(100 * time.Millisecond)
5257

@@ -56,7 +61,10 @@ func (s *Spinner) run() {
5661
select {
5762
case <-s.done:
5863
_, _ = fmt.Fprintf(writer, "\r")
64+
mu.Lock()
65+
// writes to global variable, need a global lock to avoid race conditions
5966
_ = writer.Flush()
67+
mu.Unlock()
6068
close(s.done)
6169
return
6270
case <-ticker.C:

0 commit comments

Comments
 (0)