Bot randomly exiting process or going offline/unresponsive
See original GitHub issueWhich package is this bug report for?
discord.js
Issue description
After around 5-6 hours of running, my discord bot decides to randomly exit with an exit code of 0. There is nothing in my code that should cause that, and adding a debug event listener shows the bot failing around a websocket reacquisition near the 6 hour mark.
I have tried running my bot with typescript, I’ve tried compiling the code to javascript and running that, I’ve tried packaging it as a docker container and running that locally, I’ve tried packing it as a docker container and running that on my production server. Every environment and method results in the same issue.
Bot code
https://github.com/NeonWizard/sockbot-discord
Logs
[WS => Shard 0] Heartbeat acknowledged, latency of 86ms.
[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 89ms.
[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 85ms.
[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 83ms.
[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 84ms.
[WS => Shard 0] [RECONNECT] Discord asked us to reconnect
[WS => Shard 0] [DESTROY]
Close Code : 4000
Reset : false
Emit DESTROYED: true
[WS => Shard 0] Clearing the heartbeat interval.
[WS => Shard 0] [WebSocket] Destroy: Attempting to close the WebSocket. | WS State: OPEN
[WS => Shard 0] [WebSocket] Close: Tried closing. | WS State: CLOSING
[WS => Shard 0] [WebSocket] Adding a WebSocket close timeout to ensure a correct WS reconnect.
Timeout: 5000ms
[WS => Shard 0] [WebSocket] Clearing the close timeout.
[WS => Shard 0] [WebSocket] Close Emitted: false
[WS => Shard 0] [WebSocket] did not close properly, assuming a zombie connection.
Emitting close and reconnecting again.
[WS => Shard 0] [CLOSE]
Event Code: 1011
Clean : false
Reason : INTERNAL_ERROR
[WS => Shard 0] Session id is present, attempting an immediate reconnect...
[WS => Shard 0] [CONNECT]
Gateway : wss://gateway.discord.gg/
Version : 10
Encoding : json
Compression: none
[WS => Shard 0] Setting a HELLO timeout for 20s.
[WS => Shard 0] [CONNECTED] Took 146ms
[WS => Shard 0] Clearing the HELLO timeout.
[WS => Shard 0] Setting a heartbeat interval for 41250ms.
[WS => Shard 0] [RESUME] Session REDACTED, sequence 51
[WS => Shard 0] [RESUMED] Session REDACTED | Replayed 1 events.
[WS => Shard 0] [ResumeHeartbeat] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 88ms.
[WS => Shard 0] Clearing the heartbeat interval.
[WS => Shard 0] [CLOSE]
Event Code: 4000
Clean : true
Reason :
[WS => Shard 0] Session id is present, attempting an immediate reconnect...
[WS => Shard 0] An open connection was found, attempting an immediate identify.
[WS => Shard 0] [RESUME] Session REDACTED, sequence 52
root@ruby:/home/spooky#
Code sample
https://github.com/NeonWizard/sockbot-discord
Package version
discord.js@14.1.2
Node.js version
v16.16.0, typescript@4.7.4
Operating system
Windows (WSL), Linux (Ubuntu), Docker (node:lts-alpine)
Priority this issue should have
High (immediate attention needed)
Which partials do you have configured?
No Partials
Which gateway intents are you subscribing to?
Guilds, GuildMessages, GuildMessageReactions, MessageContent
I have tested this issue on a development release
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:11
- Comments:71 (39 by maintainers)
Top GitHub Comments
My Findings and Pinging Test Bot
@NeonWizard So to test the theory of Discord 14.x potentially having issues with stability (bots remaining online), I went and created a ping bot that uses discord 14.5.0 which pinged a discord channel every 15 minutes it was online. I also logged to the discord channel whenever the bot logged in – and it had a setInterval to try automatically destroying the client and instantiating a new one.
Here’s a log of today’s results, starting from 12:25 AM. You’ll notice that no more than 3 pings are sent when the bot remained online (meaning it disconnected around an hour in, since 3 pings at 15 minutes each is 45 minutes). So it looks like there might be some timer that’s disconnecting the bot after an hour. Redacted the bot name and tag.
I stopped gathering data at 10:11 PM. So this bot was offline for an hour after that last ping.
Upon looking into the client instance, I saw something with a timeout of one hour – going by the observation that my bot did not give more than 3 pings.
Under
rest.requestManager
there also looked to behandlerTimer
which had an idleTimeout value of one hour.When my pinging bot disconnected, on replit this is what I saw in the console:
This means that the client status was online, just that the user is null (i.e. bot is not logged in).
So it’s not an issue with the client instance from what I see, it’s that the user (bot) is getting disconnected. And the idle timeout value of one hour looks suspicious to me considering the three 15 minute pings I was seeing at most at any given time the bot was online.
Not only that, but even when my code was checking to log back in every 2 minutes, the bot could take over an hour to go online again.
What a successful login looked like
When I was actually able to log in the bot, there were several web socket manager debug logs. I removed the other statements to focus on just the WS output.
Note: WS is WebSocketManager https://discord.js.org/#/docs/discord.js/main/class/WebSocketManager https://discord.js.org/#/docs/discord.js/main/class/WebSocketShard
Findings in Thread
Some other findings in the this thread:
The common data point seems to be that somewhere between v13.8.1 and v13.10.3, something introduced this issue of bots disconnecting from the client. And then that ended up getting into v14.x
You can see the diff between v13.8.1 and v13.10.3 here: https://github.com/discordjs/discord.js/compare/13.8.1...13.10.3
Update: My bot is going zombie mode about every 1-2 hours now.