From 30cfcac41666bda0f9291ef90e5609ff12d4b2ff Mon Sep 17 00:00:00 2001 From: Lucas Winther Date: Thu, 12 Nov 2026 14:45:39 +0100 Subject: [PATCH] protondrive: retry a token refresh that failed with a server error or timeout When the access token expired and the refresh request got a 5xx, 429 or 408, the operation failed instead of being retried, and a 408 even ended the session as if the refresh token had been rejected. Such failures are now retried like any other call. Co-Authored-By: Claude Opus 5.5 --- backend/protondrive/session.go | 9 ++++++--- backend/protondrive/session_internal_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/backend/protondrive/session.go b/backend/protondrive/session.go index c26d7025c..fc34b3a5b 100644 --- a/backend/protondrive/session.go +++ b/backend/protondrive/session.go @@ -213,7 +213,9 @@ func (s *session) call(ctx context.Context, opts *rest.Opts, request, response a return resp, err } if refreshErr := s.refresh(ctx, creds.AccessToken); refreshErr != nil { - return resp, refreshErr + // Not the 401's response, so that a retry is decided by the + // refresh's error. + return nil, refreshErr } if opts.Body != nil { seeker, ok := opts.Body.(io.Seeker) @@ -351,9 +353,10 @@ func (s *session) refresh(ctx context.Context, rejected string) error { }, &out, &cur) if err != nil { // As the SDK does, treat a client error other than rate - // limiting as the refresh token being rejected. + // limiting, or a timeout, as the refresh token being rejected. var apiErr *api.Error - if errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500 && apiErr.Status != http.StatusTooManyRequests { + if errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500 && + apiErr.Status != http.StatusTooManyRequests && apiErr.Status != http.StatusRequestTimeout { // Another user of the store may have spent the same refresh // token and not saved its replacement yet. if stored, ok := s.waitForStoredRefresh(ctx, cur.RefreshToken); ok { diff --git a/backend/protondrive/session_internal_test.go b/backend/protondrive/session_internal_test.go index bee05c61f..331990ae9 100644 --- a/backend/protondrive/session_internal_test.go +++ b/backend/protondrive/session_internal_test.go @@ -580,3 +580,17 @@ func TestRefreshDuringLogout(t *testing.T) { assert.Equal(t, credentials{}, store.Load(), "tokens saved after signing out") assert.Equal(t, credentials{}, s.current()) } + +func TestRefreshTransientErrorRetried(t *testing.T) { + for _, status := range []int{http.StatusServiceUnavailable, http.StatusRequestTimeout} { + t.Run(http.StatusText(status), func(t *testing.T) { + c, srv, _ := newTestClient(t) + ctx := context.Background() + srv.ExpireAccessToken(c.s.current().UID) + srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Status: status, Times: 1}) + _, _, err := c.about(ctx) + require.NoError(t, err, "a %d on the refresh wasn't retried", status) + assert.Equal(t, 2, srv.Requests()["POST /auth/v4/refresh"]) + }) + } +}