{"record":{"id":"4119e6a278f0d36a","repo":"tinyhumansai/openhuman","slug":"openrouter-oauth-was-cancelled","errorCode":null,"errorMessage":"OpenRouter OAuth was cancelled.","messagePattern":"OpenRouter OAuth was cancelled\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"app/src/utils/openrouterOAuth.ts","lineNumber":131,"sourceCode":"  // sibling OAuthProviderButton flow, which trusts the bound port).\n  parsed.hostname = 'localhost';\n  return parsed.toString();\n}\n\nexport async function connectOpenRouterViaOAuth(deps: OpenRouterOAuthDeps = {}): Promise<string> {\n  const startLoopbackListener = deps.startLoopbackListener ?? startLoopbackOauthListener;\n  const openExternalUrl = deps.openExternalUrl ?? openUrl;\n  const fetchImpl = deps.fetchImpl ?? fetch;\n  const signal = deps.signal;\n\n  const loopback = await startLoopbackListener({ port: OPENROUTER_LOOPBACK_PORT });\n  if (!loopback) {\n    throw new Error('OpenRouter OAuth requires the desktop app. Use an API key instead.');\n  }\n\n  if (signal?.aborted) {\n    await loopback.cancel();\n    throw new Error('OpenRouter OAuth was cancelled.');\n  }\n\n  const verifier = randomVerifier();\n  const challenge = await createCodeChallenge(verifier);\n  const authUrl = new URL(OPENROUTER_AUTH_URL);\n  authUrl.searchParams.set('callback_url', toOpenRouterCallbackUrl(loopback.redirectUri));\n  authUrl.searchParams.set('code_challenge', challenge);\n  authUrl.searchParams.set('code_challenge_method', PKCE_METHOD);\n\n  try {\n    await openExternalUrl(authUrl.toString());\n    const callbackUrl = await Promise.race([\n      loopback.awaitCallback(),\n      new Promise<string>((_, reject) => {\n        if (!signal) return;\n        const onAbort = () => {\n          signal.removeEventListener('abort', onAbort);\n          reject(new Error('OpenRouter OAuth was cancelled.'));","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/tinyhumansai/openhuman/blob/749120085864ce16e0f273c7b86fac7740b39c5b/app/src/utils/openrouterOAuth.ts#L113-L149","documentation":"Pre-flight cancellation check in connectOpenRouterViaOAuth: immediately after the loopback listener binds, the function inspects deps.signal?.aborted and, if the caller already aborted, cancels the listener and throws. It represents an expected control-flow exit — the user (or component teardown) cancelled the OAuth attempt before the external browser was opened.","triggerScenarios":"An AbortController tied to the flow is aborted between calling connectOpenRouterViaOAuth and the listener binding completing — user closes the sign-in dialog instantly, the component unmounts (React StrictMode double-mount cleanup) and its effect aborts, or an overall timeout signal fires.","commonSituations":"React effects that create an AbortController per mount and abort on cleanup; StrictMode's mount/unmount/mount cycle in dev; a race between the user hitting Cancel and the listener binding; tests that abort the signal before awaiting.","solutions":["Treat this as expected cancellation, not an error: catch it and show a neutral 'cancelled' state instead of an error banner or Sentry report","Don't abort on mere unmount if the sign-in should survive navigation — abort only on explicit user cancel","Create one AbortController per flow attempt and never reuse it after abort","Check `signal.aborted` before even starting the flow to avoid binding then immediately tearing down the listener"],"exampleFix":"// before\nuseEffect(() => {\n  const ctrl = new AbortController();\n  connectOpenRouterViaOAuth({ signal: ctrl.signal }).catch(reportError);\n  return () => ctrl.abort();\n}, []);\n\n// after\nuseEffect(() => {\n  const ctrl = new AbortController();\n  connectOpenRouterViaOAuth({ signal: ctrl.signal }).catch(err => {\n    if (err instanceof Error && err.message.includes('cancelled')) return; // expected\n    reportError(err);\n  });\n  return () => ctrl.abort();\n}, []);","handlingStrategy":"try-catch","validationCode":"if (signal?.aborted) {\n  return; // don't even bind the loopback listener\n}\nconst key = await connectOpenRouterViaOAuth({ signal });","typeGuard":null,"tryCatchPattern":"try {\n  const key = await connectOpenRouterViaOAuth({ signal: ctrl.signal });\n} catch (err) {\n  if (err instanceof Error && err.message.includes('cancelled')) {\n    setCancelledState(); // expected control flow — no error UI, no Sentry\n    return;\n  }\n  throw err;\n}","preventionTips":["Classify 'cancelled' as an expected outcome, not an error — filter it from error reporting","Abort only on explicit user cancel, not on every unmount, if the sign-in should survive navigation","Check signal.aborted before starting the flow to avoid binding and immediately tearing down the listener"],"tags":["oauth","abort","cancellation","abortsignal"],"backgroundTag":"operation-aborted","analyzedSha":"749120085864ce16e0f273c7b86fac7740b39c5b","analyzedAt":"2026-08-17T21:21:45.363Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}