PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
791 stars 163 forks source link

Dirty cache in websocket.py when reconnect to network #242

Open duffy356 opened 2 years ago

duffy356 commented 2 years ago

I have encountered an issue with reconnects or changes of the network while running the bot.

When the network connection is dead for less than one minute, the cached connections from the websocket may still work, but when the network is offline for more than 5 minutes, then the websocket will reconnect, but the previous connections from the websocket._cache dictionary are outdated and dead. The Library will not connect automatically to the previously connected channels.

I could fix it for my concrete usage with an overloaded version of the following method in Client's subclass:

async def event_ready(self):
    # We are logged in and ready to chat and use commands...
    self._logger.info(f'Logged in on Twitch as | {self.nick}')
    if len(self._connection._cache) > 1:
        keys_to_delete = []
        for key in self._connection._cache.keys():
            if key != "duffy356":
                self._logger.info(f"logged in with {key} in cache")
                keys_to_delete.append(key)

        for key in keys_to_delete:
            del self._connection._cache[key]
        self._logger.info(f"removed all users from cache that were outdated!")

It would be nice to automatically reconnect to the connected channels from the websocket's cache or at least remove the entries from the cache, so that a new call of join_channels("a") will allow the library to make a new connection to channel "a".

gitagogaming commented 2 years ago

I assume this has not been resolved? I do believe this is an issue I'm encountered now..

when my PC is put to sleep for more than a few minutes then when coming back online the chatbot is no longer connected.. although some parts of it do seem to still be active which is a bit odd.

alkim0 commented 1 year ago

Just to verify, #364 does not resolve this, correct?

EDIT: It does not.

Nevrai commented 1 year ago

I’m having the same issue.