Skip to content

Commit

Permalink
fix race condition with changing max
Browse files Browse the repository at this point in the history
The Go Race Detector pointed out a data race between
the methods ChangeMax64 and State or GetMax64.
State already takes the lock, but others did not.
  • Loading branch information
mxey committed May 7, 2024
1 parent 304f5f4 commit b6dcc23
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,17 @@ func New64(max int64) *ProgressBar {

// GetMax returns the max of a bar
func (p *ProgressBar) GetMax() int {
p.lock.Lock()
defer p.lock.Unlock()

return int(p.config.max)
}

// GetMax64 returns the current max
func (p *ProgressBar) GetMax64() int64 {
p.lock.Lock()
defer p.lock.Unlock()

return p.config.max
}

Expand All @@ -635,13 +641,17 @@ func (p *ProgressBar) ChangeMax(newMax int) {
// but takes in a int64
// to avoid casting
func (p *ProgressBar) ChangeMax64(newMax int64) {
p.lock.Lock()

p.config.max = newMax

if p.config.showBytes {
p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max),
p.config.useIECUnits)
}

p.lock.Unlock() // so p.Add can lock

p.Add(0) // re-render
}

Expand Down

0 comments on commit b6dcc23

Please sign in to comment.