Skip to content

fix(auth): Cognito calls time out after a few seconds - #2072

Merged
amarkanday merged 1 commit into
mainfrom
fix/2027-cognito-timeouts
Nov 20, 2026
Merged

amarkanday merged 1 commit into
mainfrom
fix/2027-cognito-timeouts

Conversation

@amarkanday

Copy link
Copy Markdown
Contributor

Resolves #2027

What and why

The Cognito client gives up after 2 s to connect and 3 s to read, with one retry. When Cognito does not answer or is busy, a signed-in request answers 503 with Retry-After and "Sign-in service is not answering. Try again in a moment." instead of 401, so the dashboard keeps the stored sign-in rather than discarding it, and the next page load after Cognito answers again restores the session. A token Cognito refuses still answers 401.

Changes:

  • auth_service.py: a botocore Config on the Cognito client (connect 2 s, read 3 s, standard retry mode, total_max_attempts 2). New CognitoUnavailable (not a ValueError), raised only for a failed GetUser: botocore network and HTTP client errors, and a ClientError with a throttle or server-error code (TooManyRequestsException, ThrottlingException, InternalErrorException, ServiceUnavailable) or a 5xx status. Everything else keeps today's path (ValueError, then 401).
  • deps.get_current_user, GET /auth/me and POST /auth/logout answer 503 with Retry-After: 2 for it. The results WebSocket keeps close code 4401.
  • The log line names the botocore class and, for a ClientError, its error code. Never the message.

No setting, no migration, no change to the dashboard's code (two new jest tests pin its current behaviour), no OpenAPI change (make openapi leaves the three snapshots unchanged). Other Cognito calls (sign-in, refresh, sign-up and so on) get the same timeouts and keep their error mapping. On the dashboard, a page load or reload while Cognito does not answer still lands on /login, with a message naming HTTP 503 and the request id, and keeps the token; making the dashboard retry instead is a separate change.

Step-0 premise re-check at 373744b (main, after #2010)

Premise At main True?
Only one boto3.client("cognito-idp" in backend/ and modules/, built lazily in the client property auth_service.py:172 yes
The client is built with no Config (botocore's defaults) auth_service.py:172 yes
get_user_with_groups and get_user turn every failure into ValueError auth_service.py yes
deps.get_current_user turns every failure of the Cognito lookup into 401 deps.py:285-293 yes
/me and logout catch only CognitoTokenRefused and ValueError auth.py get_user_info, _sign_out_of_cognito yes
WebSocket admission maps any HTTPException to 4401 websocket_results._admit_websocket yes
read_scope and _invite_viewer need no change they call get_current_user; _invite_viewer catches HTTPException yes

Verification

Property Gate Plant (defect that makes it fail)
Client carries the exact Config test_the_cognito_client_has_finite_timeouts, test_the_real_cognito_client_carries_the_config drop config=; a longer connect timeout; max_attempts in place of total_max_attempts
A never-answering endpoint gives 503 after exactly 2 attempts test_a_never_answering_cognito_gives_503_after_two_attempts (127.0.0.1 listener, OS-picked port) 3 attempts; keep the flattening to ValueError
Bad tokens stay 401 test_a_bad_token_stays_401, test_the_classifier_reads_a_hand_built_client_error_without_raising every ClientError unavailable; read ResponseMetadata with []
Throttles and 5xx give 503 test_throttles_and_cognito_server_errors_give_503 drop the code list; drop the 5xx rule
Each botocore class maps test_a_cognito_read_timeout_is_not_a_bad_token catch only ReadTimeoutError
Token refusal unchanged test_a_refused_token_stays_401_not_503; existing test_cognito_token_claims.py, test_auth_service.py and the integration test_cognito_token_audience.py wrap the refusal in CognitoUnavailable; a refused token answers 503 in deps
Every caller /me, logout, read scope, WebSocket 4401 (test_cognito_unavailable.py); invite preview (modules/backend/tests/unit/api/test_invite_preview_cognito_unavailable.py) drop the /me clause; drop the logout clause (separately)
The dashboard client keeps the token on a JSON 503; a 401 still redirects api-503-keeps-token.test.ts (jest; reads the stored token, no spy on clearToken) the 5xx path clears the token
A 503 from /users/me under a Cognito config leaves the session anonymous, names HTTP 503, keeps the token session-check-503-keeps-token.test.tsx (jest) the session check clears the token on any failure
The log names the class and code, no token test_the_log_names_the_class_and_code_and_no_token remove the line; log the message; log the class only

Regression tests (@pytest.mark.regression) fail on the old code: test_the_cognito_client_has_finite_timeouts, test_the_real_cognito_client_carries_the_config, test_a_cognito_read_timeout_is_not_a_bad_token and test_a_never_answering_cognito_gives_503_after_two_attempts. The first two see no Config on the old code, the third a 401 where a 503 belongs, and the last more than two attempts.

NOT VERIFIED

  • Real Cognito latency, and whether 2 s / 3 s is generous enough: our deployments run local authentication, so real Cognito latency was not exercised.
  • How each dashboard page renders a mid-session 503.
  • The slow-endpoint test uses a local listener that accepts and never replies, so it covers the read timeout; a connect attempt that never completes is covered only by a stub.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NpxPyav9uBxNuiXsDvD8qn

The Cognito client gives up after 2 s to connect and 3 s to read, with one
retry. When Cognito does not answer or is busy, a signed-in request answers
503 with Retry-After and "Sign-in service is not answering. Try again in a
moment." instead of 401, so the dashboard keeps the stored sign-in rather
than discarding it, and the next page load after Cognito answers again
restores the session. A token Cognito refuses still answers 401.

- auth_service: botocore Config on the client (connect 2 s, read 3 s,
  standard retry mode, total_max_attempts 2). New CognitoUnavailable,
  raised only for a failed GetUser: botocore network and HTTP client
  errors, and a ClientError with a throttle or server-error code or a 5xx
  status. Every other failure keeps its ValueError, then 401.
- deps.get_current_user, GET /auth/me and POST /auth/logout answer 503 with
  Retry-After: 2 for CognitoUnavailable. The results WebSocket keeps close
  code 4401.
- The log line names the botocore class and a ClientError's error code,
  never the message.

Resolves #2027

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NpxPyav9uBxNuiXsDvD8qn
Signed-off-by: amarkanday <a.markanday@gmail.com>
@amarkanday amarkanday added the bug Something isn't working label Nov 20, 2026
@amarkanday
amarkanday merged commit da66e16 into main Nov 20, 2026
71 checks passed
@amarkanday
amarkanday deleted the fix/2027-cognito-timeouts branch November 20, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cognito calls time out after a few seconds

1 participant