Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

bisync: fix race when CaptureOutput is used concurrently #8815

Nick Craig-Wood2f6a6c823352
diff --git a/cmd/bisync/bilib/output.go b/cmd/bisync/bilib/output.go
index 00ac8d78e..f83b6bd07 100644
--- a/cmd/bisync/bilib/output.go
+++ b/cmd/bisync/bilib/output.go
@@ -4,15 +4,19 @@ package bilib
import (
"bytes"
"log/slog"
+ "sync"
"github.com/rclone/rclone/fs/log"
)
// CaptureOutput runs a function capturing its output at log level INFO.
func CaptureOutput(fun func()) []byte {
+ var mu sync.Mutex
buf := &bytes.Buffer{}
oldLevel := log.Handler.SetLevel(slog.LevelInfo)
log.Handler.SetOutput(func(level slog.Level, text string) {
+ mu.Lock()
+ defer mu.Unlock()
buf.WriteString(text)
})
defer func() {
@@ -20,5 +24,7 @@ func CaptureOutput(fun func()) []byte {
log.Handler.SetLevel(oldLevel)
}()
fun()
+ mu.Lock()
+ defer mu.Unlock()
return buf.Bytes()
}