Repository navigation
fix(copilot): guard token refresh with asyncio.Lock to prevent race condition - #4684
Merged
Re-bin merged 1 commit intoJul 4, 2026
Merged
Conversation
…ondition _get_copilot_access_token had a check-then-act race: concurrent chat() calls after token expiry both fetched new tokens and clobbered each other. Add asyncio.Lock with double-checked locking so only one fetch happens per expiry window. Closes HKUDS#4677
axelray-dev
marked this pull request as ready for review
July 2, 2026 20:03
Re-bin
approved these changes
Jul 4, 2026
Re-bin
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. This is a focused provider-local concurrency fix: Copilot token refresh now uses a lock with a second cache check after acquisition, so concurrent requests do not race into duplicate token exchanges.
Validated locally:
- python -m pytest tests/providers/test_github_copilot_concurrent_token.py -q
- python -m ruff check nanobot/providers/github_copilot_provider.py tests/providers/test_github_copilot_concurrent_token.py
GitHub Actions is green across the matrix.
This was referenced Jul 5, 2026
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.
Problem
GitHubCopilotProvider._get_copilot_access_token()has a check-then-act race condition with no synchronization. When the Copilot access token expires (~30 min), concurrentchat()/chat_stream()calls both see the expired token and independently fetch new tokens from GitHub's token endpoint. The last writer wins, and the loser's in-flight request may use a stale or mismatched token.Closes #4677.
Root cause
The provider is constructed once and shared across all agent turns. The check-and-refresh critical section in
_get_copilot_access_token()readsself._copilot_access_token/self._copilot_expires_at, then fetches and writes them back without a lock. When two concurrent requests arrive after token expiry, both pass the freshness check, both issue HTTP calls to the token endpoint, and both overwriteself._copilot_access_token,self.api_key, andclient.api_key.Fix
Add an
asyncio.LocktoGitHubCopilotProviderand use double-checked locking in_get_copilot_access_token():This ensures only one token fetch happens per expiry window, even under concurrent load.
Tests
test_concurrent_token_refresh_fetches_once: two concurrent calls with an expired token trigger exactly one HTTP fetchtest_second_call_returns_cached_token_while_first_in_flight: if task A is mid-fetch, task B waits for the lock, finds the cached token, and skips the HTTP calltest_copilot_token_lock_exists: provider has anasyncio.Lockinstance