Skip to content

Give the per-user refresh worker one owner - #7354

Merged
jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:credential-read-4-refresh-worker-owner
Nov 27, 2026
Merged

jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:credential-read-4-refresh-worker-owner

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

A review of the delegated credential work found problems in the per-user refresh worker that "Bound per-user upstream token reads by context" (#7351) introduced. The worker had no deadline of its own. Who records a failed refresh was decided by two racing ctx.Err() checks, which could log a failure twice or not back it off at all. And an access token that expired while its ID token was being refreshed could be handed out.

  • The detached refresh worker runs under its own one-minute deadline. This bounds the refresher's row resolution and the worker's own storage lookups; the provider call is still bounded by the refresher's 30-second timeout.
  • Exactly one side records the outcome of an abandoned ID-token refresh. The caller's select decides: if it got the result, it records it; if its context ended first, it marks the work abandoned and the worker records it. The read still returns an error matching both ErrRefreshFailed and the context error.
  • Whether a refresh was ID-token-only is passed in explicitly. Before, it was re-derived from the row's ExpiresAt, which misread a storage ErrExpired row with no expiry, and silently dropped failures whose access token expired during the wait.
  • An ID-token-only refresh that fails never returns the stored credential once its access token has expired, whether or not the caller was still waiting; the read returns the refresh error.
  • Docs (Go doc and docs/arch/11-auth-server-storage.md) no longer tell callers to log a failure category instead of the error text, which neither the service nor its only caller did. They also describe which background failures are logged and backed off.

Type of change

  • Bug fix

Test plan

  • Unit tests (task test)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Ran go test -race over ./pkg/auth/upstreamtoken/... and ./pkg/authserver/.... New synctest tests cover the worker deadline with a hung refresher, abandoned-refresh recording (exactly one WARN, backoff applied), a zero-expiry ErrExpired row failing after the caller left, and the access token expiring during an ID-token refresh with the caller waiting and with the caller gone. The deadline, ID-token-only and expiry tests were each checked to fail with their fix removed.

Does this introduce a user-facing change?

For embedders reading through InProcessService: before, if an ID-token-only refresh failed and the access token had expired during that refresh, GetValidTokensForUser, GetValidTokens and GetAllUpstreamCredentials returned the stored, now-expired credential with no error. Now they return the refresh error, as they do for a failed access-token refresh. A failed background refresh after the caller left is logged once at WARN instead of possibly twice.

Special notes for reviewers

  • One ordering can't be tested deterministically: the caller receiving the result and its context ending before it checks. There is no storage call in that gap to hook. The callerLeftRefresh doc explains why the decision uses the abandoned signal and not ctx.Err().
  • If the caller got a failure and its context ended right after, it still records the backoff, but resolves the row ID on that ended context. Under Redis that lookup can fail and fall back to the session key, which makes the backoff less effective. I left it rather than add a storage call with no bound on the caller's goroutine.

The per-user refresh worker ran without a deadline, so a hung row
resolution inside the refresher, or the worker's own suppression
lookups, could keep the goroutine alive indefinitely. It now runs under
its own one-minute deadline, longer than the refresher's refresh
timeout, which still bounds the provider call.

Who recorded a refresh outcome was decided by two independent checks of
ctx.Err(), one in the worker after sending and one in the caller. They
raced: a caller that saw the failure and was then cancelled produced a
second WARN, and a worker that saw ctx live while the caller then saw it
cancelled left the backoff unrecorded. The caller's select now decides:
if it receives the result it records it; if ctx ends first it marks the
work abandoned and the worker records it. The read returns an internal
sentinel for that case, still matching ErrRefreshFailed and ctx.Err().

Whether a refresh was ID-token-only was re-derived from the row's
ExpiresAt at failure time, which misread a storage ErrExpired row with
no expiry as ID-token-only and dropped failures whose access token
expired during the wait. The call site now passes it explicitly.

An ID-token-only refresh no longer returns the stored credential when
its access token expired during the refresh, whether or not the caller
was still waiting; the read returns the refresh error instead.

The docs no longer tell callers to log a failure category rather than
error text, which neither the service nor its caller did, and describe
which background failures are logged and backed off.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 27, 2026 13:07
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.96970% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.60%. Comparing base (35c677f) to head (158cf72).

Files with missing lines Patch % Lines
pkg/auth/upstreamtoken/service.go 96.96% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7354   +/-   ##
=======================================
  Coverage   82.59%   82.60%           
=======================================
  Files         943      943           
  Lines       96943    96952    +9     
=======================================
+ Hits        80074    80089   +15     
+ Misses      16864    16858    -6     
  Partials        5        5           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against #7351.

  • Ownership handoff: sound. done is unbuffered, and on every path the caller either receives the result or closes abandoned. So exactly one side records the outcome and the worker never blocks, including when the result and ctx.Done() are ready at the same moment.
  • idTokenOnly: passing it explicitly fixes the zero-expiry ErrExpired row.
  • Expiry check in refreshForIDToken: it runs before both the caller-left return and the stored-credential fallback. No expired access token is handed out, on the per-user path or the session paths.
  • Session-mode callers: they change only in the case the PR describes. A provider whose access token expired during a failed ID-token refresh now goes to the failed map and gets a 401, which is what a failed refresh of an expired access token already did.
  • Worker deadline: it doesn't cut a save short. RefreshAndStore runs the provider call and the store under its own WithoutCancel with a 30s timeout.

go test -race passes on ./pkg/auth/upstreamtoken/... and ./pkg/authserver/..., and the upstreamtoken package also passes five repeated runs.

@jhrozek
jhrozek merged commit a4ad728 into stacklok:main Nov 27, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants