Skip to content

Close websocket connection before asyncio.run() terminates#315

Merged
RealCLanger merged 1 commit intopytr-org:masterfrom
yubiuser:fix/asyncio
Mar 28, 2026
Merged

Close websocket connection before asyncio.run() terminates#315
RealCLanger merged 1 commit intopytr-org:masterfrom
yubiuser:fix/asyncio

Conversation

@yubiuser
Copy link
Copy Markdown
Contributor

I've noticed that the websocket connection is not properly closed before asyncio.run() terminates. This resulted in the following error

10:27:07 Nothing to download.
Fatal error on SSL transport
protocol: <asyncio.sslproto.SSLProtocol object at 0x722be3ff6c20>
transport: <_SelectorSocketTransport closing fd=9>
Traceback (most recent call last):
  File "asyncio/selector_events.py", line 924, in write
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "asyncio/sslproto.py", line 690, in _process_write_backlog
  File "asyncio/selector_events.py", line 930, in write
  File "asyncio/selector_events.py", line 725, in _fatal_error
  File "asyncio/selector_events.py", line 737, in _force_close
  File "asyncio/base_events.py", line 753, in call_soon
  File "asyncio/base_events.py", line 515, in _check_closed
RuntimeError: Event loop is closed

This PR added async def close() method to TradeRepublicApi in api.py that gracefully closes the websocket. The new method is now used at the end of each async loop.

Now it shuts down without error.

Signed-off-by: yubiuser <github@yubiuser.dev>
Copy link
Copy Markdown
Collaborator

@RealCLanger RealCLanger left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for the contribution.

@RealCLanger RealCLanger merged commit 540e6aa into pytr-org:master Mar 28, 2026
5 checks passed
@yubiuser yubiuser deleted the fix/asyncio branch March 28, 2026 12:43
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