#7175·woodpecker

OAuth refresh still reuses rotated refresh tokens when a caller holds a stale user copy (Bitbucket Cloud: refresh_token is invalid)

Author: healdropperCreated Sep 23, 2026Updated Sep 23, 2026
Labelsbug

Component

server

Describe the bug

Follow-up to #5839 / #6153. The singleflight added in #6153 only merges refreshes that overlap in time. A caller that loaded the user from the store just before another request finished refreshing and persisting a new token pair still sees the old expiry in its own copy, enters forge.Refresh after the singleflight has completed, and refreshes again with the old, already rotated refresh token.

With forges that use single-use refresh tokens this breaks the user permanently. Bitbucket Cloud has rotated refresh tokens since April 2026, and on our instance the server log shows, repeatedly:

ERR refresh oauth token of user '<login>' failed error="oauth2: \"unauthorized_client\" \"refresh_token is invalid\""

From then on every operation using that user's token fails for all repos the user activated (branch list: failed to load branches: Token is invalid, expired, or not supported for this endpoint, repo repair, clone netrc, commit statuses) until the user logs in again. Users that activated many busy repos are hit much more often, because webhooks and status updates use their token concurrently.

I intend to submit a PR for this.

Related: #5917 (repair keeps using the activating user's token). It is not fixed by this, but this bug is a common way to reach that state.

Steps to reproduce

  1. Bitbucket Cloud forge, a user who activated several busy repos.
  2. Let the access token approach expiry while requests keep using it (pushes, UI).
  3. Request A refreshes and stores the new pair. Request B loaded the user before A stored it and calls forge.Refresh right after A's singleflight has returned.
  4. B sends the rotated refresh token to Bitbucket and gets refresh_token is invalid. The user's repos fail until re-login.

Expected behavior

A refresh already completed by another request is reused. The stale refresh token is never sent to the forge.

System Info

bash
v3.18.1 (refresh code unchanged on main)

Additional context

v3.18.1 (refresh code unchanged on main)

Validations

Source: woodpecker-ci/woodpecker