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

oauthutil: fix Renew.Shutdown nil timer panic and race

TastyHeadphones80e462d48c18
diff --git a/lib/oauthutil/renew.go b/lib/oauthutil/renew.go
index 71357ebf5..aaf941b1f 100644
--- a/lib/oauthutil/renew.go
+++ b/lib/oauthutil/renew.go
@@ -87,7 +87,12 @@ func (r *Renew) Shutdown() {
// closing a channel can only be done once
r.shutdown.Do(func() {
if r.ts != nil {
- r.ts.expiryTimer.Stop()
+ // expiryTimer is created under ts.mu by OnExpiry; may still be nil.
+ r.ts.mu.Lock()
+ if r.ts.expiryTimer != nil {
+ r.ts.expiryTimer.Stop()
+ }
+ r.ts.mu.Unlock()
}
close(r.done)
})
diff --git a/lib/oauthutil/renew_test.go b/lib/oauthutil/renew_test.go
new file mode 100644
index 000000000..d05f2d915
--- /dev/null
+++ b/lib/oauthutil/renew_test.go
@@ -0,0 +1,43 @@
+package oauthutil
+
+import (
+ "context"
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/rclone/rclone/fs/config/configmap"
+)
+
+func newTestTokenSource(t *testing.T) *TokenSource {
+ t.Helper()
+ tok, err := json.Marshal(map[string]any{
+ "access_token": "A",
+ "token_type": "bearer",
+ "refresh_token": "R",
+ "expiry": time.Now().Add(time.Hour).UTC().Format(time.RFC3339),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, ts, err := NewClient(context.Background(), "repro", configmap.Simple{"token": string(tok)},
+ &Config{TokenURL: "https://127.0.0.1:1/token"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return ts
+}
+
+// Shutdown before the renewOnExpiry goroutine calls OnExpiry must not panic.
+func TestShutdownBeforeRenewerStarts(t *testing.T) {
+ for i := 0; i < 1000; i++ {
+ NewRenew("repro", newTestTokenSource(t), func() error { return nil }).Shutdown()
+ }
+}
+
+// Shutdown must not race OnExpiry's write of expiryTimer.
+func TestShutdownRacesOnExpiry(t *testing.T) {
+ r := NewRenew("repro", newTestTokenSource(t), func() error { return nil })
+ time.Sleep(50 * time.Millisecond)
+ r.Shutdown()
+}