Skip to content

Reconnect immediately if connection fails on token expiry - #44

Merged
steventux merged 3 commits into
mainfrom
fix/reconnect-immediately-on-token-expiry
Mar 11, 2026
Merged

Reconnect immediately if connection fails on token expiry#44
steventux merged 3 commits into
mainfrom
fix/reconnect-immediately-on-token-expiry

Conversation

@steventux

Copy link
Copy Markdown
Contributor

Description

Token expiry is set to 3600 seconds, you do the math. This means that when the token expires we currently raise a ConnectionClosedError which is caught in the main exception handler, this pauses for 5 seconds before reconnecting.

We want to reconnect immediately so this PR amends the exception handling to detect ConnectionClosedError with a code of 1011 and "ExpiredToken" in the exception message and reconnect immediately.
In testing this proved quicker than attempting to reconnect ahead of expiry time due to the way the for message in websocket... loop executes.

Jira link

https://nhsd-jira.digital.nhs.uk/browse/DTOSS-12425

Review notes

Review checklist

  • Check database queries are correctly scoped to current_provider

@steventux
steventux force-pushed the fix/reconnect-immediately-on-token-expiry branch from f6daaf5 to 04b6db0 Compare March 10, 2026 16:49
@steventux
steventux force-pushed the fix/reconnect-immediately-on-token-expiry branch from 04b6db0 to 38fd146 Compare March 10, 2026 16:52
Comment thread src/relay_listener.py Outdated

@carlosmartinez carlosmartinez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the connection closes for some other reason than token expiry, retry in 5 seconds.
@steventux

Copy link
Copy Markdown
Contributor Author

@carlosmartinez I've added constants and a few more tests around other types of error.

@steventux
steventux merged commit 444a620 into main Mar 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants