#9980·rclone

oauthutil: Renew.Shutdown panics on a nil expiryTimer and races OnExpiry

Author: elimisteveCreated Sep 26, 2026Updated Sep 27, 2026

The associated forum post URL from https://forum.rclone.org

N/A — a bug in library code, with a minimal reproducer and a suggested patch below.

What is the problem you are having with rclone?

oauthutil.(*Renew).Shutdown reads r.ts.expiryTimer without holding r.ts.mu, while the renewal goroutine that NewRenew starts creates that timer under ts.mu in TokenSource.OnExpiry. Two consequences:

  1. Nil-pointer panic. If Shutdown runs before the renewal goroutine has called OnExpiry, expiryTimer is still nil and r.ts.expiryTimer.Stop() panics (renew.go:90). A backend whose Fs.Shutdown stops its token renewer (pcloud, box, onedrive all call f.tokenRenewer.Shutdown()) can hit this when an Fs is created and shut down quickly.
  2. Data race. Otherwise Shutdown (renew.go:90) races the write in OnExpiry (oauthutil.go:430); go test -race reports it.

Related but different: #9052 was a nil-pointer crash in oauthutil at startup with an expired token, fixed in d3e3af3 (v1.71). This one is in Renew.Shutdown and still reproduces on v1.75.1.

What is your rclone version (output from rclone version)

Used as a Go library: github.com/rclone/rclone v1.75.1. The code is unchanged on master as of today (Renew.Shutdown at lib/oauthutil/renew.go:83, TokenSource.OnExpiry at lib/oauthutil/oauthutil.go:426). go1.27.1, linux/amd64.

Which OS you are using and how many bits (e.g. Windows 7, 64 bit)

Linux 6.14, x86_64 (the bug is not platform-specific).

Which cloud storage system are you using? (e.g. Google Drive)

Found with pCloud; any backend whose Shutdown stops its OAuth token renewer is affected.

The command you were trying to run (e.g. rclone copy /tmp remote:tmp)

Library use: create an Fs, upload, then Shutdown. Minimal reproducer against the public API:

go
package repro

import (
	"context"
	"encoding/json"
	"testing"
	"time"

	"github.com/rclone/rclone/fs/config/configmap"
	"github.com/rclone/rclone/lib/oauthutil"
)

func newTS(t *testing.T) *oauthutil.TokenSource {
	tok, _ := json.Marshal(map[string]any{"access_token": "A", "token_type": "bearer", "refresh_token": "R",
		"expiry": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)})
	_, ts, err := oauthutil.NewClient(context.Background(), "repro", configmap.Simple{"token": string(tok)},
		&oauthutil.Config{TokenURL: "https://127.0.0.1:1/token"})
	if err != nil {
		t.Fatal(err)
	}
	return ts
}

// Panics: nil pointer dereference at lib/oauthutil/renew.go:90.
func TestShutdownBeforeRenewerStarts(t *testing.T) {
	for i := 0; i < 1000; i++ {
		oauthutil.NewRenew("repro", newTS(t), func() error { return nil }).Shutdown()
	}
}

// With -race: renew.go:90 (read) vs oauthutil.go:430 (write).
func TestShutdownRacesOnExpiry(t *testing.T) {
	r := oauthutil.NewRenew("repro", newTS(t), func() error { return nil })
	time.Sleep(50 * time.Millisecond)
	r.Shutdown()
}

A log from the command with the -vv flag (e.g. output from rclone -vv copy /tmp remote:tmp)

go test -run TestShutdownBeforeRenewerStarts:

panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xa67bdf]

goroutine 50 [running]:
testing.tRunner.func1.2({0x11be4c8, 0x12b4360})
	/snap/go/11295/src/testing/testing.go:2123 +0x232
testing.tRunner.func1()
	/snap/go/11295/src/testing/testing.go:2126 +0x329
panic({0x11be4c8?, 0x12b4360?})
	/snap/go/11295/src/runtime/panic.go:859 +0x125
time.(*Timer).Stop(...)
	/snap/go/11295/src/time/sleep.go:86
github.com/rclone/rclone/lib/oauthutil.(*Renew).Shutdown.func1()
	github.com/rclone/[email protected]/lib/oauthutil/renew.go:90 +0x1f
sync.(*Once).doSlow(0x7ee?, 0x563b20?)

go test -race -run TestShutdownRacesOnExpiry:

WARNING: DATA RACE
Read at 0x00c000351b58 by goroutine 11:
  github.com/rclone/rclone/lib/oauthutil.(*Renew).Shutdown.func1()
      github.com/rclone/[email protected]/lib/oauthutil/renew.go:90 +0x68
Previous write at 0x00c000351b58 by goroutine 12:
  github.com/rclone/rclone/lib/oauthutil.(*TokenSource).OnExpiry()
      github.com/rclone/[email protected]/lib/oauthutil/oauthutil.go:430 +0xbc
  github.com/rclone/rclone/lib/oauthutil.(*Renew).renewOnExpiry()
      github.com/rclone/[email protected]/lib/oauthutil/renew.go:41 +0x55
  github.com/rclone/rclone/lib/oauthutil.NewRenew.gowrap1()
      github.com/rclone/[email protected]/lib/oauthutil/renew.go:32 +0x2e

Suggested fix

Take the lock and allow for the timer not existing yet. Both tests above pass with -race after it:

diff
@@ -87,7 +87,13 @@
 	// closing a channel can only be done once
 	r.shutdown.Do(func() {
 		if r.ts != nil {
-			r.ts.expiryTimer.Stop()
+			// OnExpiry creates expiryTimer under ts.mu from the renewal
+			// goroutine, which may not have run yet.
+			r.ts.mu.Lock()
+			if r.ts.expiryTimer != nil {
+				r.ts.expiryTimer.Stop()
+			}
+			r.ts.mu.Unlock()
 		}
 		close(r.done)
 	})