Skip to content

Fix/token timeout 2#325

Open
philipjusher wants to merge 5 commits intomainfrom
fix/token-timeout-2
Open

Fix/token timeout 2#325
philipjusher wants to merge 5 commits intomainfrom
fix/token-timeout-2

Conversation

@philipjusher
Copy link
Copy Markdown
Collaborator

This pull request introduces improvements to authentication token handling and reliability, particularly around token refresh logic and error handling, and adds comprehensive tests for these scenarios. The main changes include extending the HTTP client retry timeout, improving automatic token refresh on authentication failures, and ensuring WebSocket reconnections use fresh tokens. Several new tests have been added to verify these behaviors.

Authentication and Token Refresh Improvements

  • Enhanced the auth_flow method in AnsysIDAuth to automatically detect a 401 Unauthorized response, force-refresh the authentication token, and retry the request with the new token. This ensures more robust handling of expired or rejected tokens.

HTTP Client Reliability

  • Increased the maximum retry timeout for HTTP gateway errors in get_http_client from 10 seconds to 120 seconds, allowing for longer retry periods during transient failures.

WebSocket and Progress Monitoring

  • Updated the progress monitoring logic to ensure a fresh token is fetched and used when reconnecting to the WebSocket after a disconnection, simulating mid-job token expiry scenarios.
  • Added the necessary import for get_ansyId_token in progress.py to support token refresh during WebSocket reconnections.
  • Cleaned up unused imports in the job progress monitoring script.

Testing Enhancements

  • Added new tests to verify that:
    • The authentication flow retries on 401 responses with a fresh token.
    • Non-401 errors do not trigger a token refresh.
    • Token refresh logic works correctly across multiple sequential requests, including mid-sequence expiry.
    • WebSocket reconnections correctly fetch and use a fresh token after a simulated disconnection. [1] [2]

These changes collectively improve the robustness of authentication and progress monitoring, ensuring seamless operation even in the face of token expiry and transient network issues.

Usher and others added 4 commits March 18, 2026 16:54
- Add 401 response handling in AnsysIDAuth.auth_flow to force-refresh
  the token and retry the request when the server rejects it as expired
- Increase stop_after_delay from 10s to 120s so the retry window is
  actually wider than the max exponential backoff interval (60s)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- test_auth_flow_retries_on_401_with_fresh_token: simulates an expired
  token by mocking the first request to return 401, verifies that
  get_ansyId_token is called a second time with force=True and the
  retry request succeeds with the fresh token
- test_auth_flow_does_not_retry_on_other_errors: asserts that non-401
  errors do not trigger a token refresh

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ng jobs

- Fix NameError: add missing get_ansyId_token import to progress.py so
  the token refresh on WebSocket reconnect (line 118) actually works at
  runtime
- test_auth_flow_token_expires_mid_sequence: sends 4 HTTP requests via
  a shared client; the 3rd returns 401 (expired token), verifies that
  auth_flow retries with force=True and requests 1, 2, 3 (retry), and 4
  all complete with 200
- test_token_refreshed_on_websocket_reconnect: simulates a long-running
  job where the WebSocket disconnects mid-job (token expiry), verifies
  that get_ansyId_token is called to fetch a fresh token and the second
  connection uses that token to receive the completion message

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot added testing Anything related to tests bug Something isn't working labels Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working testing Anything related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants