Repository navigation
fix(auth): serialize concurrent xAI OAuth write-through to global root #47460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sprmn24
wants to merge
1
commit into
NousResearch:main
from
sprmn24:fix/xai-oauth-write-through-race
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| """ | ||
|
|
||
| import json | ||
| import threading | ||
|
|
||
| import pytest | ||
|
|
||
|
|
@@ -167,3 +168,64 @@ def _exploding_save(store, target_path=None): | |
|
|
||
| profile = _read_store(profile_path) | ||
| assert profile["providers"]["xai-oauth"]["tokens"]["refresh_token"] == "r" | ||
|
|
||
|
|
||
| def test_concurrent_write_through_does_not_lose_second_token(profile_and_root): | ||
| """Concurrent write-throughs must serialize; final root holds one complete token set.""" | ||
| profile_path, root_path = profile_and_root | ||
| _write_store(profile_path, {"version": 1, "providers": {}}) | ||
| _write_store( | ||
| root_path, | ||
| { | ||
| "version": 1, | ||
| "providers": { | ||
| "xai-oauth": { | ||
| "tokens": {"access_token": "old-access", "refresh_token": "old-refresh"} | ||
| } | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| state_a = { | ||
| "tokens": {"access_token": "token-A-access", "refresh_token": "token-A-refresh"}, | ||
| "auth_mode": "oauth_pkce", | ||
| } | ||
| state_b = { | ||
| "tokens": {"access_token": "token-B-access", "refresh_token": "token-B-refresh"}, | ||
| "auth_mode": "oauth_pkce", | ||
| } | ||
|
|
||
| errors = [] | ||
| barrier = threading.Barrier(2) | ||
|
|
||
| def write_a(): | ||
| try: | ||
| barrier.wait() | ||
| auth._write_through_xai_oauth_to_global_root(state_a) | ||
| except Exception as exc: | ||
| errors.append(exc) | ||
|
|
||
| def write_b(): | ||
| try: | ||
| barrier.wait() | ||
| auth._write_through_xai_oauth_to_global_root(state_b) | ||
| except Exception as exc: | ||
| errors.append(exc) | ||
|
|
||
| t1 = threading.Thread(target=write_a) | ||
| t2 = threading.Thread(target=write_b) | ||
| t1.start() | ||
| t2.start() | ||
| t1.join() | ||
| t2.join() | ||
|
|
||
| assert not errors, f"Threads raised exceptions: {errors}" | ||
|
|
||
| root = _read_store(root_path) | ||
| final_tokens = root["providers"]["xai-oauth"]["tokens"] | ||
|
|
||
| token_a = {"access_token": "token-A-access", "refresh_token": "token-A-refresh"} | ||
| token_b = {"access_token": "token-B-access", "refresh_token": "token-B-refresh"} | ||
| assert final_tokens == token_a or final_tokens == token_b, ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This accepts the pre-fix last-writer-wins result as well: |
||
| f"Expected one complete token set (A or B), got corrupted mix: {final_tokens}" | ||
| ) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lock protects only the non-pool helper.
agent/credential_pool.py:529-537performs the same global-root read-modify-write forxai-oauthwithout taking this sidecar lock, so a pool refresh can still race this code. Please share the global-root locking mechanism with that helper.