unslothai/unsloth · error · CodexAuthError

ChatGPT connection was disconnected during refresh.

Error message

ChatGPT connection was disconnected during refresh.

What it means

Raised during a token refresh when the OAuth bundle existed before the refresh started but disappeared by the time the refresh HTTP call finished. This means the user (or another worker) disconnected/deleted the ChatGPT connection while the refresh token request was in flight, so there is nothing to persist the refreshed token into.

Source

Thrown at studio/backend/core/inference/openai_codex_auth.py:852

            previous_refresh_token = bundle["refresh_token"]
            try:
                body = await _token_request(
                    {
                        "grant_type": "refresh_token",
                        "client_id": OPENAI_CODEX_CLIENT_ID,
                        "refresh_token": previous_refresh_token,
                    }
                )
            except CodexReauthorizationRequired:
                current = load_oauth_bundle(provider_id)
                if current and current.get("refresh_token") == previous_refresh_token:
                    current["reauthorization_required"] = True
                    save_oauth_bundle(provider_id, current)
                raise
            refreshed = _validate_token_payload(body, previous_refresh_token)
            current = load_oauth_bundle(provider_id)
            if not current:
                raise CodexAuthError("ChatGPT connection was disconnected during refresh.")
            if current.get("refresh_token") != previous_refresh_token:
                return current["access_token"], current["account_id"]
            save_oauth_bundle(provider_id, refreshed)
            return refreshed["access_token"], refreshed["account_id"]

View on GitHub (pinned to 203007d190)

Solutions

  1. Treat this as an expected race: catch it and stop retrying the request — the connection is gone by user intent
  2. Prompt the user to re-connect ChatGPT if they still want Codex access
  3. If disconnects are automated, serialize them with provider_oauth_write_guard so they cannot interleave with an in-flight refresh

Example fix

// before
try { await client.stream(...); } catch (e) { /* generic retry loop re-throws forever */ }

// after
try {
  await client.stream(...);
} catch (e) {
  if (e instanceof CodexAuthError && /disconnected during refresh/.test(e.message)) {
    markConnectionDisconnected(providerId); // stop retrying, prompt reconnect
    return;
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try:
    token, account = await refresh_codex_token(provider_id)
except CodexAuthError as exc:
    if "disconnected during refresh" in str(exc):
        mark_connection_disconnected(provider_id)
        notify_user_reconnect_required()
        return
    raise

Prevention

When it happens

Trigger: Inside the provider_oauth_write_guard section of the token refresh: previous bundle existed, _token_request succeeded, _validate_token_payload passed, but load_oauth_bundle(provider_id) now returns None. Caused by disconnect/delete of the provider racing the refresh.

Common situations: User clicks 'Disconnect' in the Studio settings while a Codex request is streaming and triggers a refresh; a cleanup job or account removal deletes OAuth storage mid-refresh; multi-worker shared DB where one worker tears the connection down.

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/056cdd7c5a1e8334. Report an issue: GitHub.