Skip to content

Improve error handling for depth manager #1579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 21, 2025

Conversation

pcriadoperez
Copy link
Collaborator

@pcriadoperez pcriadoperez commented Apr 20, 2025

Problem:

  • Currently the depth manager if the underlying socket closes, does not notify the user
  • No error was returned on a cancelled loop

Solution

  • Improves error handling for depth manager and threaded manager.
  • Notifies user in the case of a disconnect and allows the user to ignore so websocket automatically reconnects
  • Improved logging
  • Add docs for error handling
  • Added examples for both depth manager and threaded depth manager

Note for backward compatibility: Previous users were told to expect to recieve a None in case of an error and now we are returning a message containing an error

@pcriadoperez
Copy link
Collaborator Author

Logs now received showing process of disconnect, reconnect, and closing of a websocket

INFO:__main__:Price: 85122.19, Quantity: 0.48748
INFO:__main__:Price: 85122.2, Quantity: 0.21281
INFO:__main__:Last update time: 1745182962014
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:websockets.client:% sending keepalive ping
DEBUG:websockets.client:> PING c4 4d e2 f4 [binary, 4 bytes]
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:websockets.client:- timed out waiting for keepalive pong
DEBUG:websockets.client:> CLOSE 1011 (internal error) keepalive ping timeout [24 bytes]
DEBUG:websockets.client:= connection is CLOSING
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
DEBUG:binance.ws.reconnecting_websocket:no message in 10 seconds
WARNING:binance.ws.depthcache:Exception recieving message: TimeoutError (e) 
DEBUG:websockets.client:< EOF
DEBUG:websockets.client:> EOF
DEBUG:websockets.client:= connection is CLOSED
ERROR:binance.ws.reconnecting_websocket:BinanceWebsocketClosed (Connection closed. Reconnecting...)
DEBUG:binance.ws.reconnecting_websocket:websocket reconnecting. 4 reconnects left - waiting 2
DEBUG:binance.ws.depthcache:Received message: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
DEBUG:binance.ws.depthcache:Received depth event: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
ERROR:binance.ws.depthcache:Error in depth event restarting cache: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
ERROR:__main__:Received depth cache error in callback: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
DEBUG:binance.ws.depthcache:Exiting depth cache manager for BTCUSDT
DEBUG:binance.ws.reconnecting_websocket:Closing Websocket wss://stream.binance.com:9443/ws/btcusdt@depth
DEBUG:binance.ws.reconnecting_websocket:Establishing new WebSocket connection
ERROR:binance.ws.reconnecting_websocket:Failed to connect to websocket: [Errno 8] nodename nor servname provided, or not known
DEBUG:binance.ws.reconnecting_websocket:websocket reconnecting. 3 reconnects left - waiting 1
DEBUG:binance.ws.reconnecting_websocket:Establishing new WebSocket connection
ERROR:binance.ws.reconnecting_websocket:Failed to connect to websocket: [Errno 8] nodename nor servname provided, or not known
DEBUG:binance.ws.reconnecting_websocket:websocket reconnecting. 2 reconnects left - waiting 6
DEBUG:binance.ws.reconnecting_websocket:Establishing new WebSocket connection
ERROR:binance.ws.reconnecting_websocket:Failed to connect to websocket: [Errno 8] nodename nor servname provided, or not known
DEBUG:binance.ws.reconnecting_websocket:websocket reconnecting. 1 reconnects left - waiting 7
DEBUG:binance.ws.reconnecting_websocket:Establishing new WebSocket connection
ERROR:binance.ws.reconnecting_websocket:Failed to connect to websocket: [Errno 8] nodename nor servname provided, or not known
ERROR:binance.ws.reconnecting_websocket:Max reconnections 5 reached:
ERROR:binance.ws.reconnecting_websocket:Unknown exception: BinanceWebsocketUnableToConnect ()
DEBUG:binance.ws.reconnecting_websocket:Finished killing read_loop
DEBUG:binance.ws.reconnecting_websocket:Closing Websocket wss://ws-api.binance.com/ws-api/v3

@pcriadoperez pcriadoperez changed the title error depth Improve error handling for depth manager Apr 20, 2025
@pcriadoperez pcriadoperez self-assigned this Apr 20, 2025
@pcriadoperez pcriadoperez marked this pull request as ready for review April 20, 2025 22:59
@carlosmiei carlosmiei merged commit 94f28c9 into sammchardy:master Apr 21, 2025
10 of 14 checks passed
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