Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions xrpl/asyncio/clients/async_websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
class AsyncWebsocketClient(AsyncClient, WebsocketBase):
"""
An async client for interacting with the rippled WebSocket API.

Instead of calling ``open`` and ``close`` yourself, you
can use a context like so::

Expand Down Expand Up @@ -137,7 +136,7 @@ async def main():
streams=[StreamParameter.LEDGER],
))
print("Unsubscribed from the ledger!")
except:
except Exception:
# if you wish you perform some logic when the websocket
# connection closes due to error, you can catch and run
# whatever you need to here. this is equivalent to the
Expand Down Expand Up @@ -176,7 +175,11 @@ async def websocket_supervisor():
while True:
try:
await long_websocket_task()
except:
except KeyboardInterrupt:
# allow crtl-c to actually cancel
raise
except Exception:
# anything else reconnects
print("Lost connection! Reconnecting")
Comment on lines +178 to 183
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Excellent pattern for supervisor loop with proper cancellation support.

The explicit KeyboardInterrupt handling enables users to cleanly exit the supervisor loop with Ctrl-C, while all other exceptions trigger reconnection. This is the recommended pattern for websocket supervisors.

Minor typo: "crtl-c" → "ctrl-c" on line 179.

🤖 Prompt for AI Agents
In xrpl/asyncio/clients/async_websocket_client.py around lines 178 to 183, there
is a minor typo in the inline comment text: "crtl-c" should be "ctrl-c"; update
the comment to read "allow ctrl-c to actually cancel" (preserve
capitalization/style) so the docstring/comment is correct while leaving the
exception handling logic unchanged.



Expand All @@ -190,15 +193,18 @@ async def long_websocket_task():
# set up a listener task
listener = asyncio.create_task(on_message(client))

# subscribe to the ledger
await client.send(Subscribe(
streams=[StreamParameter.LEDGER],
))
try:
# subscribe to the ledger
await client.send(Subscribe(
streams=[StreamParameter.LEDGER],
))

# sleep infinitely until the connection closes on us
while client.is_open():
await asyncio.sleep(0)
listener.cancel()
# sleep infinitely until the connection closes on us
while client.is_open():
await asyncio.sleep(0)
finally:
# whether or not there is an error, cancel the listener
listener.cancel()


async def main():
Expand Down