xai-org/grok-build · error

Failed to clear auth: {e}

Error message

Failed to clear auth: {e}

What it means

run_cli_logout (the `grok logout` CLI handler) calls perform_logout to clear cached credentials. If perform_logout returns an error (e.g. the auth store cannot be read/written or the backend rejects the revocation), it is wrapped as 'Failed to clear auth: {e}'. The local session may still be present.

Source

Thrown at crates/codegen/xai-grok-shell/src/auth/flow.rs:987

            auth_manager.remove_scope(scope)?;
        } else {
            auth_manager.clear()?;
        }
        crate::managed_config::clear_orphan();
    }
    Ok(LogoutResult {
        was_logged_in,
        email,
        api_key_still_set: crate::agent::auth_method::has_xai_api_key_env(),
    })
}
/// `grok logout` CLI handler. Calls [`perform_logout`] and formats
/// the result to stderr.
pub fn run_cli_logout(config: &crate::agent::config::Config) -> anyhow::Result<()> {
    let grok_home = grok_home::grok_home();
    let auth_manager = AuthManager::new(&grok_home, config.grok_com_config.clone());
    let result = perform_logout(&auth_manager, None)
        .map_err(|e| anyhow::anyhow!("Failed to clear auth: {e}"))?;
    if !result.was_logged_in {
        eprintln!("No cached session to log out of.");
        if result.api_key_still_set {
            eprintln!("You are authenticated via XAI_API_KEY (environment variable).");
        }
        return Ok(());
    }
    if let Some(email) = result.email {
        eprintln!("Logged out (was signed in as {email})");
    } else {
        eprintln!("Logged out");
    }
    if result.api_key_still_set {
        eprintln!("XAI_API_KEY is still set and will be used for authentication.");
    }
    Ok(())
}
#[cfg(test)]

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Read the wrapped cause after the colon; fix that underlying issue (permissions, disk, keyring, network).
  2. If the store is corrupted, back it up and delete the auth file, then log in again.
  3. Ensure grok home is writable (`chmod 700 ~/.grok`) and a keyring/secret service is available and unlocked.
  4. If revocation failed due to network, fix connectivity and retry; the local credentials are the security concern, so clear them manually if needed.
Defensive patterns

Strategy: try-catch

Validate before calling

const home = process.env.GROK_HOME || '~/.grok';
try { fs.accessSync(home, fs.constants.W_OK | fs.constants.R_OK); }
catch { console.error('grok home unreadable/unwritable; logout will fail'); }

Try / catch

match run_cli_logout(config) {
    Err(e) if e.to_string().starts_with("Failed to clear auth") => {
        eprintln!("{e:#} — fix the root cause (permissions/keyring/corrupt store) and retry");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Running `grok logout` when the auth store is unreadable/corrupted, the grok home directory is not writable, the keyring is locked/unavailable, or the remote token revocation request fails.

Common situations: Corrupted ~/.grok auth file; read-only home in containers; keyring locked after reboot on headless Linux; network failure during server-side revocation.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/c307da235922a25e. Report an issue: GitHub.