tastytrade: AlertStreamer Hearbeat Exception

Hi, when I’m running my code using tastytrade 9.14 the AlertStreamer generates the following error from time to time while a session to Tastytrade is open:

Task exception was never retrieved
future: <Task finished name='Task-15' coro=<AlertStreamer._heartbeat() done, defined at C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\site-packages\tastytrade\streamer.py:336> exception=ConnectionClosedOK(Close(code=1000, reason='Bye'), Close(code=1000, reason='Bye'), True)>
Traceback (most recent call last):
  File "C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\site-packages\tastytrade\streamer.py", line 343, in _heartbeat
    await self._subscribe(SubscriptionType.HEARTBEAT, "")
  File "C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\site-packages\tastytrade\streamer.py", line 369, in _subscribe
    await self._websocket.send(json.dumps(message))  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\asyncio\connection.py", line 476, in send
    async with self.send_context():
  File "C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Heiko\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\asyncio\connection.py", line 957, in send_context
    raise self.protocol.close_exc from original_exc
websockets.exceptions.ConnectionClosedOK: received 1000 (OK) Bye; then sent 1000 (OK) Bye

It seems like the keepalive WebSocket connection is closed from Tastytrade without an error but with reason ConnectionClosedOK.

However this case seems not to be handled. I’d expect that the AlertStreamer tries to reestablish the WebSocket Connection again in this case.

Is any additional information required?

About this issue

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

Commits related to this issue

Most upvoted comments

However I doesn’t deliver any quotes which have been subscribed after the initial connection was established. I guess it would be required to subscribe again. I’m not sure how this should be handled. I saw there are reconnect_fn and reconnect_args parameters for the streamer classes in place. Should this be used to resubscribe?

Yup! That would be your problem. For some reason I had assumed you were already using the reconnect functionality! You can read about it here: https://tastyworks-api.readthedocs.io/en/latest/data-streamer.html#retry-callback

This code works for me even if I disable my internet for 30 seconds then turn it back on:

from tastytrade import Session, DXLinkStreamer
from tastytrade.dxfeed import Quote

session = Session("username", "password")
subs_list = ["SPY"]


async def callback(streamer: DXLinkStreamer, subs: list[str]) -> None:
    await streamer.subscribe(Quote, subs)


async with DXLinkStreamer(
    session, reconnect_args=(subs_list,), reconnect_fn=callback
) as streamer:
    await streamer.subscribe(Quote, subs_list)
    async for quote in streamer.listen(Quote):
        print(quote)