Hmbown/CodeWhale · error

xAI OAuth lifecycle lock was poisoned

Error message

xAI OAuth lifecycle lock was poisoned

What it means

This error is thrown by with_xai_oauth_lifecycle_lock when the process-wide std::sync::Mutex guarding all xAI/chatgpt OAuth credential mutations is poisoned — i.e. another thread panicked while holding the lock. Once poisoned, every subsequent lock() returns Err and this library surfaces it as an anyhow error instead of proceeding with a possibly inconsistent credential store.

Solutions

  1. Restart the process — a poisoned std mutex cannot be recovered in-process.
  2. Find and fix the panic that originally poisoned the lock (check logs for the first panic during an OAuth credential operation).
  3. If you own the calling code, avoid panics inside credential operations (return Result instead of unwrap/expect).
  4. As a workaround within the library, the lock could be re-acquired via poison recovery (into_inner), but prefer fixing the root cause over patching the library.

Example fix

// before
let creds = store.clear_all_xai_oauth_credentials()?; // fails after a poisoning panic
// after
// restart the process first; then guard the operation
match store.clear_all_xai_oauth_credentials() {
    Ok(()) => info!("credentials cleared"),
    Err(e) => error!("oauth lock poisoned or op failed: {e:#}"),
}
Defensive patterns

Strategy: try-catch

Try / catch

match store.clear_all_xai_oauth_credentials() {
    Err(e) if e.to_string().contains("lock was poisoned") => {
        eprintln!("OAuth lifecycle lock poisoned; restart the process and investigate the earlier panic");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling with_xai_oauth_lifecycle_lock (directly or via with_xai_oauth_revocation_transaction, remove_xai_oauth_generation, clear_all_xai_oauth_credentials, clear_all_chatgpt_oauth_credentials, remove_chatgpt_oauth_generation) after some thread panicked while holding the PROCESS_LOCK mutex.

Common situations: An earlier OAuth operation panicked mid-mutation (disk error unwrapping, a bug in the credential store) leaving the mutex poisoned; a long-running process where one bad request poisons the lock and all later credential operations fail.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/d2e0fad1dc51f5e8. Report an issue: GitHub.

Appendix: source

Thrown at crates/config/src/xai_credentials.rs:167

}

pub fn legacy_chatgpt_oauth_path() -> Result<PathBuf> {
    Ok(xai_oauth_credentials_dir()?.join(LEGACY_CHATGPT_OAUTH_FILE_NAME))
}

/// Serialize every Codewhale-owned xAI OAuth lifecycle mutation across threads
/// and processes while pinning the lexical credentials directory.
///
/// Lock order is always xAI lifecycle first, then config document. Callers must
/// not invoke this function recursively.
pub fn with_xai_oauth_lifecycle_lock<T>(
    operation: impl FnOnce(&XaiOAuthCredentialStore) -> Result<T>,
) -> Result<T> {
    static PROCESS_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    let _process_guard = PROCESS_LOCK
        .get_or_init(|| Mutex::new(()))
        .lock()
        .map_err(|_| anyhow::anyhow!("xAI OAuth lifecycle lock was poisoned"))?;
    let store = XaiOAuthCredentialStore::open()?;
    let lock_file = store.open_lock_file()?;
    let mut lock = fd_lock::RwLock::new(lock_file);
    let _guard = lock.write().with_context(|| {
        format!(
            "failed to acquire xAI OAuth lifecycle lock in {}",
            crate::quote_os_path(store.directory())
        )
    })?;
    operation(&store)
}

/// Run an authority mode switch while the prior owned OAuth epoch is hidden
/// from concurrent Codewhale readers. A failed authority mutation restores the
/// old files; a successful mutation permanently removes them.
pub fn with_xai_oauth_revocation_transaction<T>(
    operation: impl FnOnce() -> Result<T>,
) -> Result<T> {

View on GitHub (pinned to 73e0f67d83)