Skip to content

pkg/llm: fix withPreemptiveRefresh composition and stale _AT cache on refresh token rotation #5051

Closed

Description

@yrobla

Two issues flagged in PR review by @aponcedeleonch on the thv-llm-token branch.


1. withPreemptiveRefresh composition is broken

File: pkg/llm/tokensource.go

Both callers of withPreemptiveRefresh pass an src that is already an oauth2.ReuseTokenSource:

  • flow.TokenSource() returns oauth2.ReuseTokenSource(token, base) (flow.go:511)
  • remote.CreateTokenSourceFromCached returns oauth2.ReuseTokenSource(token, base) (persisting_token_source.go:104), then wrapped in PersistingTokenSource

The current stacking is:

outer ReuseTokenSource(nil, preemptiveTokenSource{inner: PersistingTokenSource{source: inner ReuseTokenSource{source: rawRefresher}}})

The bug: when the outer ReuseTokenSource's cached token hits the shifted expiry, it calls preemptiveTokenSource, which calls the inner chain. The inner ReuseTokenSource sees the real expiry is still valid and returns the same cached token unchanged. preemptiveTokenSource shifts the expiry again and returns the same shifted value. The outer caches it — but it immediately appears expired on the next call because time.Now() has already crossed real_expiry - 40s. Every Token() call inside the 30-second preemptive window triggers a full inner round-trip instead of one refresh per window.

Fix: wrap the raw refresher with preemptiveTokenSource before oauth2.ReuseTokenSource, not after. Target stacking:

ReuseTokenSource(nil, preemptiveTokenSource{inner: PersistingTokenSource{source: rawRefresher}})

Requires:

  • In tryRestoreFromCache: build the raw token source directly instead of using CreateTokenSourceFromCached (which adds an inner ReuseTokenSource)
  • In performBrowserFlow: build raw token source from flowCfg fields directly instead of calling flow.TokenSource() (which is a ReuseTokenSource)
  • Note: oauth2.Config.TokenSource also returns a reuseTokenSource internally, so NewResourceTokenSource (which does its own validity check + stores token field) is the correct stateless refresher for the resource path
  • Add a regression test with a fake refresher that counts calls, verifying that multiple Token() calls within the preemptive window produce exactly one inner Token() call

2. Stale _AT cache entry after refresh token rotation

File: pkg/llm/tokensource.go — makeTokenPersister

When the persister callback fires (a rotated refresh token is written to the secrets store), the associated _AT access token cache entry is left unchanged. Between that write and the next Token() call, tier 1.5 could serve an access token that was minted against the now-invalidated refresh token. The preemptive window reduces the window in practice, but it is not zero.

Fix: inside makeTokenPersister, after SetSecret succeeds, overwrite or delete the _AT entry so the next Token() call falls through to tier 2 and gets a fresh access token:

// Invalidate the access-token cache so the next call does not serve a token
// minted against the now-rotated refresh token.
_ = t.secretsProvider.SetSecret(ctx, t.accessTokenCacheKey(), "")

Or delete it if the provider supports no-op deletes for missing keys.


Both issues are on pkg/llm/tokensource.go introduced in the thv-llm-token branch / PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenticationbuggo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions