sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.99k stars 2.2k forks source link

Prevent never awaited coroutine from KeepAliveWebsocket._keepalive_socket #1166

Closed youknowone closed 2 years ago

youknowone commented 2 years ago

Though asyncio.create_task is run after timeout, self._keepalive_socket() is run right after connect. When the socket is unexpectedly canceled, this happens never-awaited coroutine error.

/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py:73: RuntimeWarning: coroutine 'KeepAliveWebsocket._keepalive_socket' was never awaited
Coroutine created at (most recent call last)
  File "/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 1882, in _run_once
    handle._run()
  File "/opt/homebrew-x86/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File
    async with self.socket_manager.futures_user_socket() as stream:
  File "/Users/user/Projects/python-binance/binance/streams.py", line 66, in __aenter__
    await self.connect()
  File "/Users/user/Projects/python-binance/binance/streams.py", line 94, in connect
    await self._after_connect()
  File "/Users/user/Projects/python-binance/binance/streams.py", line 250, in _after_connect
    self._start_socket_timer()
  File "/Users/user/Projects/python-binance/binance/streams.py", line 256, in _start_socket_timer
    self._keepalive_socket()
  self._args = None
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

This PR delays creation of coroutine after timeout.

sammchardy commented 2 years ago

Thanks @youknowone, good solution for this issue