Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling ConnectionClosedOK exception in Relay._receive_messages() #2

Open
wants to merge 2 commits into
base: electrum
Choose a base branch
from

Conversation

f321x
Copy link

@f321x f321x commented Nov 27, 2024

If a relay closes the connection to aionostr successfully (not due to an error) _receive_messages() will be stuck in a infinite exception loop and block the event loop as it doesn't catch ConnectionClosedOK exception, only the ConnectionClosedError exception.

Example:

Traceback (most recent call last):
File \"/usr/local/lib/python3.9/dist-packages/electrum_aionostr/relay.py\", line 69, in _receive_messages
     message = await asyncio.wait_for(self.ws.recv(), 30.0)
File \"/usr/lib/python3.9/asyncio/tasks.py\", line 481, in wait_for
     return fut.result()
File \"/usr/local/lib/python3.9/dist-packages/websockets/legacy/protocol.py\", line 568, in recv
     await self.ensure_open()
File \"/usr/local/lib/python3.9/dist-packages/websockets/legacy/protocol.py\", line 939, in ensure_open
     raise self.connection_closed_exc()
 websockets.exceptions.ConnectionClosedOK: received 1001 (going away) CloudFlare WebSocket proxy restarting; then sent 1001 (going away) CloudFlare WebSocket proxy restarting

This PR changes the except to catch the parent exception ConnectionClosed which will then attempt to reconnect on both, 'OK' and 'Error' closes.
Additionally it checks the return value of self.connect() when called in reconnect() to prevent trying to send if a connection attempt was unsuccessful, instead it will timeout and try again later after the 20 attempts failed.

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.

1 participant