Repository navigation
Bound per-user upstream token reads by context - #7351
Conversation
Before, GetValidTokensForUser waited for the refresher even after the caller context ended. Now it returns promptly while an in-flight refresh can still persist the row. Keep session reads synchronous so the caller can hold its generation lease until refresh completes. Let the user-keyed grant guard rely on the reader for cancellation. A failure that reaches the guard after its deadline is treated as uncertain, so the grant goes through instead of being revoked on a late not-found answer. Omit the provider-supplied token type from errors that reach operator logs. Run the refresh worker detached from the caller's cancellation, so a caller that leaves early cannot abort a refresh before the provider is called. Record the ID-token refresh suppression from the worker, for the owner's row only, after it sends its result, so a refresh that finishes after the caller left does not make the next read call the provider again. When that refresh fails after the caller left, record the failure backoff and log it once, so a failing provider is not called on every read. Document that callers should log a failure category, not error text, and what a read returns when the context ends during an ID-token-only refresh, including that a row the refresher clears in the meantime is reported on the next read. Co-authored-by: Mecatl <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7351 +/- ##
==========================================
- Coverage 82.57% 82.57% -0.01%
==========================================
Files 940 941 +1
Lines 96639 96722 +83
==========================================
+ Hits 79802 79866 +64
- Misses 16832 16851 +19
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
rdimitrov
left a comment
There was a problem hiding this comment.
Approving. This matches the reader contract in #7263 and what #6407 needs from it: when a client leaves a vMCP tool call during a refresh, the read no longer blocks, and a rotated refresh token is still saved.
Verified locally (merged with main, -race):
- The touched packages plus
./pkg/auth/...,./pkg/authserver/...and./pkg/vmcp/auth/...pass. - The new and changed reader and guard tests pass 10 runs in a row with no flakes.
- The hung-IdP guard case, which uses the real refresher, is still cut off at the check timeout now that the guard has no goroutine of its own.
WithoutCancelkeeps the remembered-rejection marker the guard sets on its context.
The trade-offs in the description look right to me: the one-read window, the guard running past its timeout by up to the Redis client's own timeout, and the two narrow races. None of them can produce a wrong grant.
rdimitrov
left a comment
There was a problem hiding this comment.
LGTM. GetValidTokensForUser is the only path that stops waiting. The session reads keep waiting because they pass an empty expectedUserID, and the user-keyed guard is the only non-test caller of the per-user reader. The detached worker can't leak or double-spend a refresh token:
- its result channel is buffered;
- the refresher's shared in-flight call is keyed by row ID, so a second read joins the running refresh;
- suppression and backoff are keyed by row ID plus ID token, so they can't attach to a row that a new sign-in replaced.
go test -race -count=3 ./pkg/auth/upstreamtoken/ ./pkg/authserver/ passes, and so do the new cancellation tests at -count=30. A downstream embedder builds against this head, and its broker tests pass.
Not blocking: the detached worker isn't tracked by shutdown, so it can touch storage up to about 30s after Close. Main's guard goroutine already outlived its caller the same way, so this is not a regression.
Summary
GetValidTokensForUser) kept waiting on the refresher after the caller's context ended, up to the refresher's 30s timeout. The refresh-grant guard worked around that with its own goroutine andselect, so any other caller of the reader would have hung. This moves the bound into the reader.GetValidTokensForUserreturns when the caller's context ends. The refresh runs in a worker detached from the caller's cancellation, so a refresh that has started can still save the rotated row. If the context ends while an expired access token refreshes, the error matches bothErrRefreshFailedand the context error.GetValidTokens,GetAllUpstreamCredentials) still wait for the refresher, because their callers hold a generation lease on the context until the read returns.token_typeerror no longer includes the provider's value.Docs:
docs/arch/11-auth-server-storage.mdand the reader's doc comments describe the new contract, including that callers should log a failure category, noterr.Error().Type of change
Test plan
task test): run on the touched packages (./pkg/auth/upstreamtoken/,./pkg/authserver/) with-raceand the Taskfile's flags, not the whole repotask test-e2e)task lint-fix)New tests cover: cancel during an expired-access-token refresh (returns promptly, row still saved), cancel during an ID-token-only refresh that then fails (one WARN, no second provider call), cancel during one where the refresher clears the row (first read returns the credential, the next needs authorization), a context that has already ended before the read, and session reads still waiting.
Changes
pkg/auth/upstreamtoken/service.gorefreshDetachedbounds the wait for per-user reads; the worker runs detached, sends its result first, then records suppression or the failure backoffpkg/auth/upstreamtoken/types.gopkg/authserver/refresh_grant_guard_user_keyed.gopkg/authserver/upstream/oauth2.gotoken_typeerror omits the provider's valuedocs/arch/11-auth-server-storage.md*_test.goDoes this introduce a user-facing change?
For operators:
unexpected token_typeerror text no longer contains the provider's value.No change for clients of the token endpoint.
Special notes for reviewers
refreshOrFailbranches onexpectedUserID.userRowSessioninuser_reader_test.golost its parameter because every caller passed the same user. It is test-only and unexported, so it does not affect embedders.