{"record":{"id":"d2e0fad1dc51f5e8","repo":"Hmbown/CodeWhale","slug":"xai-oauth-lifecycle-lock-was-poisoned","errorCode":null,"errorMessage":"xAI OAuth lifecycle lock was poisoned","messagePattern":"xAI OAuth lifecycle lock was poisoned","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/config/src/xai_credentials.rs","lineNumber":167,"sourceCode":"}\n\npub fn legacy_chatgpt_oauth_path() -> Result<PathBuf> {\n    Ok(xai_oauth_credentials_dir()?.join(LEGACY_CHATGPT_OAUTH_FILE_NAME))\n}\n\n/// Serialize every Codewhale-owned xAI OAuth lifecycle mutation across threads\n/// and processes while pinning the lexical credentials directory.\n///\n/// Lock order is always xAI lifecycle first, then config document. Callers must\n/// not invoke this function recursively.\npub fn with_xai_oauth_lifecycle_lock<T>(\n    operation: impl FnOnce(&XaiOAuthCredentialStore) -> Result<T>,\n) -> Result<T> {\n    static PROCESS_LOCK: OnceLock<Mutex<()>> = OnceLock::new();\n    let _process_guard = PROCESS_LOCK\n        .get_or_init(|| Mutex::new(()))\n        .lock()\n        .map_err(|_| anyhow::anyhow!(\"xAI OAuth lifecycle lock was poisoned\"))?;\n    let store = XaiOAuthCredentialStore::open()?;\n    let lock_file = store.open_lock_file()?;\n    let mut lock = fd_lock::RwLock::new(lock_file);\n    let _guard = lock.write().with_context(|| {\n        format!(\n            \"failed to acquire xAI OAuth lifecycle lock in {}\",\n            crate::quote_os_path(store.directory())\n        )\n    })?;\n    operation(&store)\n}\n\n/// Run an authority mode switch while the prior owned OAuth epoch is hidden\n/// from concurrent Codewhale readers. A failed authority mutation restores the\n/// old files; a successful mutation permanently removes them.\npub fn with_xai_oauth_revocation_transaction<T>(\n    operation: impl FnOnce() -> Result<T>,\n) -> Result<T> {","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/config/src/xai_credentials.rs#L149-L185","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Restart the process — a poisoned std mutex cannot be recovered in-process.","Find and fix the panic that originally poisoned the lock (check logs for the first panic during an OAuth credential operation).","If you own the calling code, avoid panics inside credential operations (return Result instead of unwrap/expect).","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."],"exampleFix":"// before\nlet creds = store.clear_all_xai_oauth_credentials()?; // fails after a poisoning panic\n// after\n// restart the process first; then guard the operation\nmatch store.clear_all_xai_oauth_credentials() {\n    Ok(()) => info!(\"credentials cleared\"),\n    Err(e) => error!(\"oauth lock poisoned or op failed: {e:#}\"),\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match store.clear_all_xai_oauth_credentials() {\n    Err(e) if e.to_string().contains(\"lock was poisoned\") => {\n        eprintln!(\"OAuth lifecycle lock poisoned; restart the process and investigate the earlier panic\");\n    }\n    other => other?,\n}","preventionTips":["Avoid panics (unwrap/expect) in code paths that run under the OAuth lifecycle lock","Log and investigate the first panic in any OAuth credential operation — later failures are secondary","Restart the process once the lock is poisoned; it cannot recover in-process","Keep credential-mutating operations small so a panic window is minimal"],"tags":["concurrency","mutex-poisoned","oauth"],"backgroundTag":"lock-poisoned","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}