coinbase / coinbase-advanced-py

The Advanced API Python SDK is a Python package that makes it easy to interact with the Coinbase Advanced API. The SDK handles authentication, HTTP connections, and provides helpful methods for interacting with the API.
https://docs.cdp.coinbase.com/advanced-trade/docs/welcome/
Apache License 2.0
111 stars 30 forks source link

1001 Going Away does not trigger on_close function #63

Open jchappe opened 2 months ago

jchappe commented 2 months ago

Receiving a Close 1001 message from websocket is not triggering the on_close function and the associated retry logic.

Here is my connection snippet:

`

def start_websocket(self):
    while True:
        client = WSClient(api_key=self.api_key, api_secret=self.signing_key, on_message=self.on_message, on_open=self.on_open, on_close=self.on_close, verbose=True, timeout=5)
        try:
            client.open()
            client.subscribe(product_ids=[], channels=["user"])
            client.run_forever_with_exception_check()
        except WSClientConnectionClosedException as e:
            logging.info(f"User Orders Websocket Connection closed! Retry attempts exhausted. {e}")
        except WSClientException as e:
            logging.info(f"User Orders Websocket Error encountered!. {e}")
        time.sleep(.2)`

And here is the logging:

2024-08-06 22:53:08 - websockets.client - DEBUG - % sending keepalive ping 2024-08-06 22:53:08 - websockets.client - DEBUG - > PING 8d 71 87 e4 [binary, 4 bytes] 2024-08-06 22:53:08 - websockets.client - DEBUG - < CLOSE 1001 (going away) [2 bytes] 2024-08-06 22:53:08 - websockets.client - DEBUG - = connection is CLOSING 2024-08-06 22:53:08 - websockets.client - DEBUG - > CLOSE 1001 (going away) [2 bytes] 2024-08-06 22:53:08 - websockets.client - DEBUG - = connection is CLOSED

github-actions[bot] commented 2 months ago

Thank you for reporting! If this is an SDK specific issue, we will look into it and get back to you soon. If this is an API related request, report it in our Advanced API Discord instead (use this invite link if it's your first time accessing the Discord).

urischwartz-cb commented 2 months ago

I believe this is because a 1001 error is not considered an unexpected connection closure and therefore the retry logic would not be triggered. See here on how ConnectionClosedOK errors are handled vs how ConnectionClosedError errors are handled.

Furthermore, your on_close function is only called when you manually close the connection, not on an error.

How often are you seeing this behavior? I will raise this internally with the the team.