fix(credential-pool): re-select in acquire_lease after a deferred single-use-token refresh - #78299
Merged
kshitijk4poor merged 1 commit intoAug 4, 2026
Conversation
…resh select() re-selects once deferred single-use-token refreshes complete; acquire_lease() performed the refresh but returned its pre-refresh answer. Since _acquire_lease_under_lock returns early exactly when a refresh is pending (if not available: return None, pending_refresh), a pool whose entries all needed a refresh always returned None — the caller failed an answerable request right after the refresh succeeded. Retry once, only when the first pass was empty and a refresh ran. Post-merge gate-sweep finding on the NousResearch#71775 salvage (NousResearch#77714).
kshitijk4poor
enabled auto-merge (rebase)
August 4, 2026 07:39
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.
Follow-up to the #71775 salvage (#77714), found by a post-merge gate sweep of that campaign's merged PRs.
Context — what this fixes, for whom
Anyone using a credential pool whose entries are single-use OAuth tokens (openai-codex, xai-oauth). If every entry in the pool needs a token refresh at the moment a lease is requested — the normal state after an idle period, and the universal state for a single-entry pool —
acquire_lease()performs the refresh and then still returnsNone. The caller sees "no credentials available" and fails a request that was fully answerable: the refresh had just succeeded and the credential was sitting there healthy.select()does not have this bug. When #71775 moved deferred refreshes outside the pool lock, it gaveselect()a re-select pass:acquire_lease()got the same refactor but not the retry:Because
_acquire_lease_under_lockreturns early (if not available: return None, pending_refresh) precisely when a refresh is pending, theNoneis guaranteed in exactly the case the refresh was meant to resolve.The fix
Mirror
select(): retry the lease once, and only when the first pass came back empty AND a refresh actually ran. No retry for a healthy pool (no wasted selection pass), and no loop when the refresh doesn't help.Verification
tests/agent/test_credential_pool_lease_refresh_reselect.py: 3 tests — the bug case, a no-double-select guard for the healthy path, and a still-returns-None guard for a failed refresh.agent/credential_pool.pyreverted to main, the bug test FAILS and the two guard tests pass; restored, all 3 pass. The regression test genuinely pins the fix.test_credential_pool.py+test_credential_pool_deferred_refresh.py+test_reset_aware_primary_restore.py+ the new file → 79 passed.Provenance
This is a gap-closing pass over the salvage campaign's merged PRs (#77714 / #77631 / #77740 all touched
credential_pool.py). Three related checks came back clean and are worth recording: every_available_entries()caller correctly unpacks the tuple (no second dead gate like #77740 fixed), thenext_available_atlock probe correctly tests ownership from a helper thread rather than same-thread — which would be vacuous under an RLock — and the RLock downgrade introduced no re-entrancy path that mutates state a caller then reads stale.