Skip to content
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

Kucoin - Unauthenticated subscription disconnects after 24 hours, fails to refresh token, and then cannot reconnect #1023

Open
vincentmele opened this issue Apr 5, 2024 · 1 comment
Labels

Comments

@vincentmele
Copy link
Contributor

vincentmele commented Apr 5, 2024

Describe the bug
Using an unauthenticated subscription to Kucoin's trade feed for a period of time (about 24 hours), Kucoin disconnects the websocket and then cryptofeed is unable to reconnect due to cryptofeed not refreshing the connection token.

To Reproduce
Steps to reproduce the behavior:

  • Unauthenticated KuCoin feed on TRADE channel.
  • Allow to run for 24+ hours.
2024-04-05 10:47:36,818 : WARNING : KUCOIN: error from exchange {'id': '660fd6c84d0eab582c237b51', 'type': 'error', 'code': 401, 'data': 'token is expired'}
2024-04-05 10:47:36,819 : WARNING : KUCOIN.ws.3: encountered connection issue no close frame received or sent - reconnecting in 1.0 seconds...

Expected behavior
Cryptofeed should renew the token, reconnect, and continue the channel subscriptions.

Operating System:

  • Ubuntu 22.04

Cryptofeed Version

  • cryptofeed 2.4.0 installed via pip / pypi

I see that the Binance exchange code has functions to refresh the auth token, I'll take a look and see if I can get something similar to work with KuCoin and submit a PR if successful.

@vincentmele vincentmele added the bug label Apr 5, 2024
@vincentmele vincentmele changed the title Kucoin - Kucoin - Unauthenticated subscription disconnects after 24 hours, fails to refresh token, and then cannot reconnect Apr 5, 2024
@vincentmele
Copy link
Contributor Author

Further investigation:

This appears to be because the KuCoin Feed object (exchanges/kucoin.py) only obtains the connection token once upon its creation in the "address_info" object.

    def __init__(self, **kwargs):
        address_info = self.http_sync.write('https://api.kucoin.com/api/v1/bullet-public', json=True)
        token = address_info['data']['token']
        address = address_info['data']['instanceServers'][0]['endpoint']
        address = f"{address}?token={token}"
        self.websocket_endpoints = [WebsocketEndpoint(address, options={'ping_interval': address_info['data']['instanceServers'][0]['pingInterval'] / 2000})]
        super().__init__(**kwargs)
        if any([len(self.subscription[chan]) > 300 for chan in self.subscription]):
            raise ValueError("Kucoin has a limit of 300 symbols per connection")
        self.__reset()

Then, when the connection is kicked about 24 hours later, connection_handler.py tries to reconnect without reinitializing the KuCoin Feed object.

In essence, the feed itself needs to be recreated, not just the connection being retried under the same feed. In an ideal world, the code would know that the token timeout is 24 hours, refresh it ahead of time, then restart the connection using the new token.

I've worked around this by using a process manager that restarts the feed after a set interval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant