When using client.run('token'), the bot will randomly stop/close after some time with no exceptions. It seems to occur randomly, and I can't discern a cause for it. This even occurs when running the example scripts, so it can't be something wrong with my specific code either. Any idea what's going on?
Enable logging, see here, and you'll see more information.
The last log is always "DEBUG:discord.gateway: Keeping websocket alive with sequence 48." No warnings or errors are logged before closing, and nothing specific seems to trigger it.
A sequence of 48 is insanely low. Provide the log file. If you don't want it public you can join the support guild and join the #python_discord-py channel.
I can reproduce the issue, but the log file (below) does not show any sign of the bot disconnecting.
I replaced a time.sleep(1) by await asyncio.sleep(1) inside a background task and I no longer have the problem. I didn't went and check why in the code, but if it can help anyone in the future...
asyncio is single threaded, it uses cooperative multi-tasking, meaning when you await etc it yields the thread to another task. If you use time.sleep, it freezes the entire thread, not letting anything else progress. asyncio.sleep, on the other hand, will just pause the task, allowing other tasks to continue.
Thanks for the information !
I have also noticed this a few times on my bots but have not enabled debugging yet. What I do know is that it is exiting gracefully as its called my shutdown functions but not my exception functions.
I have the same issue and it is regardless of you use token or couple of login/password. At the beginning I thought the issue was related to my bot, but watching this issue helps me reconsider...
Issue closed. Has the bug been fixed somehow?
There is no issue.
It can't be reproduced.
If you have an issue and it can be reproduced (and isn't an issue with your internet) then by all means make your own issue.
For future reference for anyone coming across this, this seems to be the remote server closing the connection, causing the discord.Client's coroutine to terminate without exceptions.
Here's a disconnect from our logs while running idly:
[2018-02-06 06:40:02,298] (DEBUG) websockets.protocol: client << Frame(fin=True, opcode=8, data=b'\x03\xe8')
[2018-02-06 06:40:02,298] (DEBUG) websockets.protocol: client >> Frame(fin=True, opcode=8, data=b'\x03\xe8')
[2018-02-06 06:40:02,305] (WARNING) redacted.bootstrap: Closing event loop...
Per RFC6455, opcode 0x8 is "close", received from the remote side (I think, not too familiar with websockets and its logging format). This seems to cause discord.py to terminate the run() or connect() coroutine gracefully, so no exceptions are thrown. Solution would normally be to implement reconnect logic.
Previous logs were just a series of user presence updates from Discord's API.
(Came across this looking for examples of anyone implement reconnect/resilience to network hiccoughs, since discord.Client seems not to like me simply trying to re-login()/connect(), back to searching. :V )
@Laogeodritt there's thankfully reconnection logic implemented for these cases in the rewrite of discord.py already. The async branch however is unlikely to get any of the new reconnection logic added to it as it's no longer going to be getting new feature updates.
What do you mean by the async branch?
I think that reconnecting when there is a remote connection shutdown is more a bugfix than a feature.
In your terminal, do: pip show discord.py
If the version is 0.16.x, it is the async branch.
If the version is 1.0.0a, it is the rewrite branch.
So as it stands the version that is installed by pip does not work and I have to clone the repo to get the 1.0.0 version that we hope works and no plans to fix a bug ?
The version on pip is feature-frozen. No new features, just bugfixes to existing features (Unless said bugfixes are a breaking change). The development branch will become main at some point in the near future. The version on pip works, it's just missing a couple bits.
Use a process manager/service manager to restart the bot when it shuts down due to this. Or, you could get a head start on getting familiar with the new branch and use that, which as mentioned, will reconnect in the event of remote host disconnecting.
Most helpful comment
@Laogeodritt there's thankfully reconnection logic implemented for these cases in the rewrite of discord.py already. The async branch however is unlikely to get any of the new reconnection logic added to it as it's no longer going to be getting new feature updates.