Skip to content

Commit

Permalink
Merge pull request #198 from gabe565/fix-print-text-race
Browse files Browse the repository at this point in the history
fix: Acquire lock when printing text to fix race condition
  • Loading branch information
schollz authored Sep 15, 2024
2 parents 03fc4e9 + ffc015d commit e57a85d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,8 @@ func shouldCacheOutput(pb *ProgressBar) bool {
}

func Bprintln(pb *ProgressBar, a ...interface{}) (int, error) {
pb.lock.Lock()
defer pb.lock.Unlock()
if !shouldCacheOutput(pb) {
return fmt.Fprintln(pb.config.writer, a...)
} else {
Expand All @@ -1161,6 +1163,8 @@ func Bprintln(pb *ProgressBar, a ...interface{}) (int, error) {
}

func Bprintf(pb *ProgressBar, format string, a ...interface{}) (int, error) {
pb.lock.Lock()
defer pb.lock.Unlock()
if !shouldCacheOutput(pb) {
return fmt.Fprintf(pb.config.writer, format, a...)
} else {
Expand Down

0 comments on commit e57a85d

Please sign in to comment.