Speed up MCP OAuth credential reads (#37860)

## Why

Refreshing MCP connection identities should not stall the async executor while another process holds the OAuth credential-store lock.

## What changed

- Probe file and secrets credential stores without waiting during runtime refreshes, retaining the last known credentials when the store is contended.
- Reuse unchanged authentication failures while credentials are temporarily unavailable instead of restarting the connection.
- Run blocking credential loads on a blocking worker during MCP client construction.
- Allow OAuth fallback when a configured authorization header is missing, empty, or invalid.

GitOrigin-RevId: 38dad5606da9a1ceb30fe945000ec9151a5058fa
This commit is contained in:
jif authored and copyberry committed 2026-08-10 17:58:15 +00:00
1 parent d06dc73290
commit afcc95b431
7 files changed
+329 -44

No files matched your search

+89 -4
View File
@@ -72,6 +72,7 @@ use codex_utils_home_dir::find_codex_home;
pub(crate) use self::resolved_store::ResolvedOAuthCredentialStore;
pub(crate) use self::resolved_store::ResolvedOAuthTokens;
pub(crate) use self::resolved_store::resolve_oauth_tokens_from_store_policy;
use self::resolved_store::try_resolve_oauth_tokens_from_store_policy;
const KEYRING_SERVICE: &str = "Codex MCP Credentials";
const MCP_OAUTH_SECRET_PREFIX: &str = "MCP_OAUTH";
@@ -88,10 +89,17 @@ pub struct StoredOAuthTokens {
}
/// OAuth credentials paired with the concrete store selected for their client lifecycle.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub struct StoredOAuthCredentialSnapshot {
credentials: StoredOAuthTokens,
store: ResolvedOAuthCredentialStore,
store_was_contended: bool,
}
impl PartialEq for StoredOAuthCredentialSnapshot {
fn eq(&self, other: &Self) -> bool {
self.credentials == other.credentials && self.store == other.store
}
}
impl StoredOAuthCredentialSnapshot {
@@ -100,7 +108,49 @@ impl StoredOAuthCredentialSnapshot {
&self.credentials
}
/// Rereads the selected authority, allowing Auto login to migrate File credentials to keyring.
/// Returns whether this snapshot was retained because its store could not be read.
pub fn store_was_contended(&self) -> bool {
self.store_was_contended
}
/// Refreshes a runtime snapshot without waiting or discarding its last known authority.
pub fn for_runtime_refresh(
previous: Option<&Self>,
server_name: &str,
url: &str,
store_mode: OAuthCredentialsStoreMode,
keyring_backend_kind: AuthKeyringBackendKind,
) -> Result<Option<Self>> {
match try_resolve_oauth_tokens_from_store_policy(
&DefaultKeyringStore,
server_name,
url,
store_mode,
keyring_backend_kind,
) {
Ok(Some(mut resolved)) => {
resolved.tokens.token_response.0.set_expires_in(None);
Ok(Some(Self {
credentials: resolved.tokens,
store: resolved.store,
store_was_contended: false,
}))
}
Ok(None) => Ok(None),
Err(error) if oauth_store_is_contended(&error) => Ok(previous
.filter(|previous| {
previous.credentials.server_name == server_name
&& previous.credentials.url == url
})
.map(|previous| Self {
store_was_contended: true,
..previous.clone()
})),
Err(error) => Err(error),
}
}
/// Rereads the selected authority without waiting for a contended credential-store lock.
pub fn reload(
&self,
server_name: &str,
@@ -111,10 +161,21 @@ impl StoredOAuthCredentialSnapshot {
if self.store == ResolvedOAuthCredentialStore::File
&& store_mode == OAuthCredentialsStoreMode::Auto
{
return stored_oauth_credentials(server_name, url, store_mode, keyring_backend_kind);
return Self::for_runtime_refresh(
/*previous*/ None,
server_name,
url,
store_mode,
keyring_backend_kind,
)
.map(|snapshot| snapshot.map(|snapshot| snapshot.credentials));
}
let credentials = self.store.load(&DefaultKeyringStore, server_name, url)?;
let credentials = match self.store.try_load(&DefaultKeyringStore, server_name, url) {
Ok(credentials) => credentials,
Err(error) if oauth_store_is_contended(&error) => return Ok(None),
Err(error) => return Err(error),
};
Ok(normalized_oauth_credentials(credentials.as_ref()))
}
}
@@ -194,9 +255,18 @@ pub fn stored_oauth_credential_snapshot(
Ok(Some(StoredOAuthCredentialSnapshot {
credentials,
store: resolved.store,
store_was_contended: false,
}))
}
fn oauth_store_is_contended(error: &Error) -> bool {
matches!(
error.downcast_ref::<OAuthStoreLockFailure>(),
Some(OAuthStoreLockFailure::Timeout { acquire_timeout, .. })
if acquire_timeout.is_zero()
)
}
fn normalized_oauth_credentials(tokens: Option<&StoredOAuthTokens>) -> Option<StoredOAuthTokens> {
tokens.map(|tokens| {
let mut tokens = tokens.clone();
@@ -283,6 +353,14 @@ fn load_oauth_tokens_from_secrets_keyring<K: KeyringStore + Clone + 'static>(
url: &str,
) -> std::result::Result<Option<StoredOAuthTokens>, OAuthKeyringLoadError> {
let _store_lock = OAuthStoreLock::acquire_for_read(OAuthStore::Secrets)?;
load_oauth_tokens_from_secrets_keyring_with_lock_held(keyring_store, server_name, url)
}
fn load_oauth_tokens_from_secrets_keyring_with_lock_held<K: KeyringStore + Clone + 'static>(
keyring_store: &K,
server_name: &str,
url: &str,
) -> std::result::Result<Option<StoredOAuthTokens>, OAuthKeyringLoadError> {
let codex_home = find_codex_home().map_err(anyhow::Error::from)?;
let manager = SecretsManager::new_with_keyring_store_and_namespace(
codex_home.to_path_buf(),
@@ -682,6 +760,13 @@ struct FallbackTokenEntry {
fn load_oauth_tokens_from_file(server_name: &str, url: &str) -> Result<Option<StoredOAuthTokens>> {
let _store_lock = OAuthStoreLock::acquire_for_read(OAuthStore::File)?;
load_oauth_tokens_from_file_with_lock_held(server_name, url)
}
fn load_oauth_tokens_from_file_with_lock_held(
server_name: &str,
url: &str,
) -> Result<Option<StoredOAuthTokens>> {
let Some(store) = read_fallback_file_unlocked()? else {
return Ok(None);
};
@@ -8,13 +8,18 @@ use codex_keyring_store::KeyringStore;
use tracing::warn;
use super::OAuthKeyringLoadError;
use super::OAuthStore;
use super::OAuthStoreLock;
use super::OAuthStoreLockFailure;
use super::StoredOAuthTokens;
use super::compute_store_key;
use super::delete_oauth_tokens_from_direct_keyring;
use super::delete_oauth_tokens_from_file;
use super::delete_oauth_tokens_from_secrets_keyring;
use super::load_oauth_tokens_from_file;
use super::load_oauth_tokens_from_file_with_lock_held;
use super::load_oauth_tokens_from_keyring;
use super::load_oauth_tokens_from_secrets_keyring_with_lock_held;
use super::save_oauth_tokens_to_file;
use super::save_oauth_tokens_with_keyring;
@@ -57,6 +62,34 @@ impl ResolvedOAuthCredentialStore {
}
}
/// Reads the selected authority without waiting for its aggregate-store lock.
pub(crate) fn try_load<K: KeyringStore + Clone + 'static>(
self,
keyring_store: &K,
server_name: &str,
url: &str,
) -> Result<Option<StoredOAuthTokens>> {
match self {
Self::File => {
let _store_lock = OAuthStoreLock::try_acquire_for_read(OAuthStore::File)?;
load_oauth_tokens_from_file_with_lock_held(server_name, url)
.context("failed to probe OAuth tokens from resolved file storage")
}
Self::Keyring(AuthKeyringBackendKind::Direct) => {
self.load(keyring_store, server_name, url)
}
Self::Keyring(AuthKeyringBackendKind::Secrets) => {
let _store_lock = OAuthStoreLock::try_acquire_for_read(OAuthStore::Secrets)?;
load_oauth_tokens_from_secrets_keyring_with_lock_held(
keyring_store,
server_name,
url,
)
.map_err(anyhow::Error::from)
}
}
}
/// Saves credentials only to this already-resolved authority.
pub(crate) fn save<K: KeyringStore + Clone + 'static>(
self,
@@ -174,3 +207,32 @@ pub(crate) fn resolve_oauth_tokens_from_store_policy<K: KeyringStore + Clone + '
})),
}
}
pub(crate) fn try_resolve_oauth_tokens_from_store_policy<K: KeyringStore + Clone + 'static>(
keyring_store: &K,
server_name: &str,
url: &str,
store_mode: OAuthCredentialsStoreMode,
keyring_backend_kind: AuthKeyringBackendKind,
) -> Result<Option<ResolvedOAuthTokens>> {
let load = |store: ResolvedOAuthCredentialStore| {
store
.try_load(keyring_store, server_name, url)
.map(|tokens| tokens.map(|tokens| ResolvedOAuthTokens { tokens, store }))
};
let keyring = ResolvedOAuthCredentialStore::Keyring(keyring_backend_kind);
match store_mode {
OAuthCredentialsStoreMode::File => load(ResolvedOAuthCredentialStore::File),
OAuthCredentialsStoreMode::Keyring => load(keyring),
OAuthCredentialsStoreMode::Auto => match load(keyring) {
Ok(Some(tokens)) => Ok(Some(tokens)),
Ok(None) => load(ResolvedOAuthCredentialStore::File),
Err(error) if error.downcast_ref::<OAuthStoreLockFailure>().is_some() => Err(error),
Err(error) => {
warn!("failed to read OAuth tokens from keyring: {error}");
load(ResolvedOAuthCredentialStore::File)
.with_context(|| format!("failed to read OAuth tokens from keyring: {error}"))
}
},
}
}
+17 -4
View File
@@ -59,15 +59,28 @@ enum OAuthStoreLockMode {
impl OAuthStoreLock {
pub(super) fn acquire_for_write(store: OAuthStore) -> Result<Self, OAuthStoreLockFailure> {
Self::acquire_with_mode(store, OAuthStoreLockMode::Exclusive)
Self::acquire_with_timeout(
store,
STORE_LOCK_ACQUIRE_TIMEOUT,
OAuthStoreLockMode::Exclusive,
)
}
pub(super) fn acquire_for_read(store: OAuthStore) -> Result<Self, OAuthStoreLockFailure> {
Self::acquire_with_mode(store, OAuthStoreLockMode::Shared)
Self::acquire_with_timeout(
store,
STORE_LOCK_ACQUIRE_TIMEOUT,
OAuthStoreLockMode::Shared,
)
}
fn acquire_with_mode(
pub(super) fn try_acquire_for_read(store: OAuthStore) -> Result<Self, OAuthStoreLockFailure> {
Self::acquire_with_timeout(store, Duration::ZERO, OAuthStoreLockMode::Shared)
}
fn acquire_with_timeout(
store: OAuthStore,
acquire_timeout: Duration,
mode: OAuthStoreLockMode,
) -> Result<Self, OAuthStoreLockFailure> {
// This lock intentionally follows the existing local File/Secrets credential-store
@@ -77,7 +90,7 @@ impl OAuthStoreLock {
// provide its own matching lock authority instead of using this local path.
let codex_home = find_codex_home()
.map_err(|source| OAuthStoreLockFailure::CodexHome { store, source })?;
Self::acquire_in_with_mode(&codex_home, store, STORE_LOCK_ACQUIRE_TIMEOUT, mode)
Self::acquire_in_with_mode(&codex_home, store, acquire_timeout, mode)
}
fn acquire_in_with_mode(
+28 -16
View File
@@ -1025,19 +1025,27 @@ impl RmcpClient {
&& auth_provider.is_none()
&& !default_headers.contains_key(AUTHORIZATION)
{
if let Some(store) = pinned_credential_store.get().copied() {
// Rebuilds reread the source selected during first construction. Only the
// initial construction below evaluates configured store policy.
store
.load(&DefaultKeyringStore, server_name, url)?
.map(|tokens| ResolvedOAuthTokens { tokens, store })
} else {
let oauth_server_name = server_name.clone();
let oauth_url = url.clone();
let oauth_store_mode = *store_mode;
let oauth_keyring_backend_kind = *keyring_backend_kind;
let pinned_credential_store = Arc::clone(pinned_credential_store);
tokio::task::spawn_blocking(move || -> Result<Option<ResolvedOAuthTokens>> {
if let Some(store) = pinned_credential_store.get().copied() {
// Rebuilds reread the source selected during first construction. Only
// initial construction below evaluates configured store policy.
return store
.load(&DefaultKeyringStore, &oauth_server_name, &oauth_url)
.map(|tokens| tokens.map(|tokens| ResolvedOAuthTokens { tokens, store }));
}
match resolve_oauth_tokens_from_store_policy(
&DefaultKeyringStore,
server_name,
url,
*store_mode,
*keyring_backend_kind,
&oauth_server_name,
&oauth_url,
oauth_store_mode,
oauth_keyring_backend_kind,
) {
Ok(tokens) => {
if let Some(resolved) = tokens.as_ref() {
@@ -1045,18 +1053,22 @@ impl RmcpClient {
// first concrete source so Auto is not reevaluated mid-client.
pinned_credential_store.set(resolved.store).map_err(|_| {
anyhow!(
"OAuth credential store pinned concurrently for MCP server `{server_name}`"
"OAuth credential store pinned concurrently for MCP server `{oauth_server_name}`"
)
})?;
}
tokens
Ok(tokens)
}
Err(err) => {
warn!("failed to read tokens for server `{server_name}`: {err}");
None
warn!(
"failed to read tokens for server `{oauth_server_name}`: {err}"
);
Ok(None)
}
}
}
})
.await
.map_err(|error| anyhow!("OAuth credential loading task failed: {error}"))??
} else {
None
};