From 4df88191c636c7d4995f3747bfce108742efbeb9 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Mon, 15 Jun 2015 03:33:49 -0700 Subject: [PATCH] fix race in pgbar printing --- cp-bar.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cp-bar.go b/cp-bar.go index 774d5d0c..d83fec12 100644 --- a/cp-bar.go +++ b/cp-bar.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "strings" + "sync" "sync/atomic" "time" @@ -127,7 +128,9 @@ func newCpBar() barSend { eraseCurrentLine := fmt.Sprintf("%c[2K\r", 27) cursorDown := fmt.Sprintf("%c[%dB", 27, 1) firstTime := true + barLock := &sync.Mutex{} bar.Callback = func(s string) { + barLock.Lock() if !firstTime { console.Print(cursorUp) console.Print(eraseCurrentLine) @@ -139,6 +142,7 @@ func newCpBar() barSend { console.Print(cursorDown) console.Print(eraseCurrentLine) console.Bar(s) + barLock.Unlock() } // Feels like wget bar.Format("[=> ]")