{"record":{"id":"056cdd7c5a1e8334","repo":"unslothai/unsloth","slug":"chatgpt-connection-was-disconnected-during-refresh","errorCode":null,"errorMessage":"ChatGPT connection was disconnected during refresh.","messagePattern":"ChatGPT connection was disconnected during refresh\\.","errorType":"exception","errorClass":"CodexAuthError","httpStatus":401,"severity":"error","filePath":"studio/backend/core/inference/openai_codex_auth.py","lineNumber":852,"sourceCode":"            previous_refresh_token = bundle[\"refresh_token\"]\n            try:\n                body = await _token_request(\n                    {\n                        \"grant_type\": \"refresh_token\",\n                        \"client_id\": OPENAI_CODEX_CLIENT_ID,\n                        \"refresh_token\": previous_refresh_token,\n                    }\n                )\n            except CodexReauthorizationRequired:\n                current = load_oauth_bundle(provider_id)\n                if current and current.get(\"refresh_token\") == previous_refresh_token:\n                    current[\"reauthorization_required\"] = True\n                    save_oauth_bundle(provider_id, current)\n                raise\n            refreshed = _validate_token_payload(body, previous_refresh_token)\n            current = load_oauth_bundle(provider_id)\n            if not current:\n                raise CodexAuthError(\"ChatGPT connection was disconnected during refresh.\")\n            if current.get(\"refresh_token\") != previous_refresh_token:\n                return current[\"access_token\"], current[\"account_id\"]\n            save_oauth_bundle(provider_id, refreshed)\n            return refreshed[\"access_token\"], refreshed[\"account_id\"]\n","sourceCodeStart":834,"sourceCodeEnd":857,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/core/inference/openai_codex_auth.py#L834-L857","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Treat this as an expected race: catch it and stop retrying the request — the connection is gone by user intent","Prompt the user to re-connect ChatGPT if they still want Codex access","If disconnects are automated, serialize them with provider_oauth_write_guard so they cannot interleave with an in-flight refresh"],"exampleFix":"// before\ntry { await client.stream(...); } catch (e) { /* generic retry loop re-throws forever */ }\n\n// after\ntry {\n  await client.stream(...);\n} catch (e) {\n  if (e instanceof CodexAuthError && /disconnected during refresh/.test(e.message)) {\n    markConnectionDisconnected(providerId); // stop retrying, prompt reconnect\n    return;\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    token, account = await refresh_codex_token(provider_id)\nexcept CodexAuthError as exc:\n    if \"disconnected during refresh\" in str(exc):\n        mark_connection_disconnected(provider_id)\n        notify_user_reconnect_required()\n        return\n    raise","preventionTips":["Serialize disconnect/delete with refreshes via the same provider_oauth_write_guard","Never auto-retry after this error — the user removed the connection intentionally","Watch connection-deleted events to cancel in-flight Codex requests early"],"tags":["auth","oauth","race-condition","codex"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}