From b6dcc237c3b096d5f4f2827b9916d6dc57d3f12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ga=C3=9F?= Date: Tue, 7 May 2024 17:40:13 +0200 Subject: [PATCH] fix race condition with changing max 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. --- progressbar.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/progressbar.go b/progressbar.go index dbdeb0a..616f6fa 100644 --- a/progressbar.go +++ b/progressbar.go @@ -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 } @@ -635,6 +641,8 @@ 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 { @@ -642,6 +650,8 @@ func (p *ProgressBar) ChangeMax64(newMax int64) { p.config.useIECUnits) } + p.lock.Unlock() // so p.Add can lock + p.Add(0) // re-render }