fix: release discovery lock before HTTP fetch; route scope errors through error handler#16
Open
fix: release discovery lock before HTTP fetch; route scope errors through error handler#16
Conversation
…rrors through error handler
- discovery: held write mutex for the entire refresh(), including the HTTP
network call, blocking all concurrent Fetch() readers. Restructured to
use singleflight (already a dependency) to coalesce concurrent cache
misses into a single fetch; the lock is now held only for the cache
check and update, matching the pattern in clientcreds.
- middleware: BearerAuth scope failures called writeInsufficientScope
directly, bypassing any custom ErrorHandler. Now routes scope errors
through cfg.errorHandler as an *oauth.Error{Code: "insufficient_scope"}.
Updated defaultErrorHandler to map insufficient_scope → 403 Forbidden
with the correct WWW-Authenticate header.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves concurrency and error-routing behavior in the SDK by (1) reducing discovery-cache contention during HTTP fetches and (2) ensuring OAuth scope failures flow through the configured middleware error handler.
Changes:
- Add
singleflight.Groupto discovery refresh to coalesce concurrent cache misses and avoid holding locks during network I/O. - Route
BearerAuthrequired-scope failures throughcfg.errorHandlerasoauth.Error{Code: "insufficient_scope"}. - Update
defaultErrorHandlerto mapinsufficient_scopeto403 Forbiddenwith an appropriateWWW-Authenticateheader.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| middleware/middleware.go | Adds insufficient_scope handling to the default error handler and routes scope failures through the configured ErrorHandler. |
| discovery/discovery.go | Adds singleflight and narrows lock scope so concurrent discovery cache misses collapse into a single HTTP request. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+209
to
+214
| return cloneMetadata(&meta), nil | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return v.(*Metadata), nil |
Comment on lines
+189
to
193
| cfg.errorHandler(w, r, &oauth.Error{ | ||
| Code: "insufficient_scope", | ||
| Description: "Token does not have required scope: " + scope, | ||
| }) | ||
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
discovery:refresh()held the write mutex across the entire function including the HTTP network call, blocking all concurrentFetch()callers for the duration of the request. Added asingleflight.Group(already a transitive dep) to coalesce concurrent cache misses into a single fetch; the lock is now held only for the cache check and update — matching the pattern already used inclientcreds.middleware:BearerAuthwas callingwriteInsufficientScopedirectly for scope failures, silently bypassing any customErrorHandler. Scope errors are now routed throughcfg.errorHandleras an*oauth.Error{Code: "insufficient_scope"}. UpdateddefaultErrorHandlerto mapinsufficient_scope → 403 Forbiddenwith the correctWWW-Authenticate: Bearer error="insufficient_scope"header.Test plan
make test— all packages passmake lint— 0 issuesTestFetch_Concurrentcovers the concurrent discovery pathTestBearerAuth_RequiredScopescovers the scope-failure path; verify custom error handler now receives scope errors🤖 Generated with Claude Code