tastytrade: Intermittent, but frequent streamer crashes on program completion

When using the code pattern:

streamer = await DXLinkStreamer(session)
await streamer.subscribe_candle(
    symbols=[....],
    interval=INTERVAL,
    start_time=datetime.datetime.now() - datetime.timedelta(...),
    extended_trading_hours=True,
)
async for candle in streamer.listen(Candle):
      # collect candles until condition
       break
await streamer.unsubscribe_candle("...", INTERVAL, extended_trading_hours=True)
await streamer.close()
print(candles...)

Often, the program runs and completes just fine, but often AFTER streamer.close(), I get an exception and statck trace (which can’t seem to be caught, no matter where I put the try/except) as follows:

[2025-01-10 11:00:14] INFO VxRatioAlert [pid: 3287]: VX is CONTANGO Front: /VXF25 19.2   Back /VXG25 19.25
[2025-01-10 11:00:14] ERROR asyncio [pid: 3287]: Fatal error on SSL transport
protocol: <asyncio.sslproto.SSLProtocol object at 0x7fd0f0746110>
transport: <_SelectorSocketTransport closing fd=8>
Traceback (most recent call last):
  File "/opt/python/3.10.15/lib/python3.10/asyncio/selector_events.py", line 924, in write
    n = self._sock.send(data)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/python/3.10.15/lib/python3.10/asyncio/sslproto.py", line 690, in _process_write_backlog
    self._transport.write(chunk)
  File "/opt/python/3.10.15/lib/python3.10/asyncio/selector_events.py", line 930, in write
    self._fatal_error(exc, 'Fatal write error on socket transport')
  File "/opt/python/3.10.15/lib/python3.10/asyncio/selector_events.py", line 725, in _fatal_error
    self._force_close(exc)
  File "/opt/python/3.10.15/lib/python3.10/asyncio/selector_events.py", line 737, in _force_close
    self._loop.call_soon(self._call_connection_lost, exc)
  File "/opt/python/3.10.15/lib/python3.10/asyncio/base_events.py", line 753, in call_soon
    self._check_closed()
  File "/opt/python/3.10.15/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

I attach a simplified version of the program. FWIW, the entirety of main() completes, with output showing up as expected, and THEN the above crash happens. This is very consistent when it crashes.

I’m unsure if there’s anything I’m doing wrong (probably re: asyncio) in this program, or if it indicates something in the library.

PS. Thanks for this lib - much nicer than the “official” one.

debug-vx-ratio.py.txt

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Have not seen that error/stack-trace since market open Sunday (2025-01-19) evening.

I’m happy to call this one solved (by putting await self._websocket.wait_closed() as the last line to DXLinkStreamer.close())

Thanks!

I actually tried the run_until_complete() thing before, and it didn’t help (even gave more obscure error messages).

However, I have locally added await self._websocket.wait_closed() as the last line to DXLinkStreamer.close() and will let you know if this cleans things up.

Cheers.

Not using a reconnect_fn. I added your suggested except Exception... to the end of the except... chain at the bottom of _connect(). Will monitor to see how that plays out. Thanks.