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

Fix race in checkServerTime

Nick Craig-Wood5454f2abd0b9
diff --git a/fs/http.go b/fs/http.go
index e2b225e2f..ffc005b98 100644
--- a/fs/http.go
+++ b/fs/http.go
@@ -143,6 +143,9 @@ func NewTransport(transport *http.Transport, logHeader, logBody bool) *Transport
}
}
+// A mutex to protect this map
+var checkedHostMu sync.RWMutex
+
// A map of servers we have checked for time
var checkedHost = make(map[string]struct{}, 1)
@@ -152,7 +155,10 @@ func checkServerTime(req *http.Request, resp *http.Response) {
if req.Host != "" {
host = req.Host
}
- if _, ok := checkedHost[host]; ok {
+ checkedHostMu.RLock()
+ _, ok := checkedHost[host]
+ checkedHostMu.RUnlock()
+ if ok {
return
}
dateString := resp.Header.Get("Date")
@@ -169,7 +175,9 @@ func checkServerTime(req *http.Request, resp *http.Response) {
if dt > window || dt < -window {
Log(nil, "Time may be set wrong - time from %q is %v different from this computer", host, dt)
}
+ checkedHostMu.Lock()
checkedHost[host] = struct{}{}
+ checkedHostMu.Unlock()
}
// RoundTrip implements the RoundTripper interface.