question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bot randomly exiting process or going offline/unresponsive

See original GitHub issue

Which 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.

image

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:open
  • Created a year ago
  • Reactions:11
  • Comments:71 (39 by maintainers)

github_iconTop GitHub Comments

6reactions
Dossarcommented, Oct 7, 2022

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.

<PingingBot>
BOT
 — Today at 12:25 AM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 12:40 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 12:55 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 1:10 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 1:29 AM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 1:43 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 1:58 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 2:13 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 6:49 AM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 7:04 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 7:19 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 7:34 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 8:34 AM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 8:49 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 10:14 AM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 10:29 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 10:44 AM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 1:29 PM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 1:44 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 1:59 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 2:14 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 3:39 PM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 3:54 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 4:09 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 4:24 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 4:44 PM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 4:59 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 5:14 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 5:29 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 5:49 PM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 6:04 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 6:19 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 6:34 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 7:59 PM
<PingingBot>#0000 successfully logged in!

<PingingBot>
BOT
 — Today at 8:14 PM
<PingingBot>#0000 successfully logged in!
NEW

<PingingBot>
BOT
 — Today at 8:29 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 8:44 PM
15 minute ping interval successfully sent.

<PingingBot>
BOT
 — Today at 8:59 PM
15 minute ping interval successfully sent.

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.

  sweepers: Sweepers {
    options: { threads: [Object] },
    intervals: {
      applicationCommands: null,
      bans: null,
      emojis: null,
      invites: null,
      guildMembers: null,
      messages: null,
      presences: null,
      reactions: null,
      stageInstances: null,
      stickers: null,
      threadMembers: null,
      threads: Timeout {
        _idleTimeout: 3600000,
        _idlePrev: [TimersList],
        _idleNext: [Timeout],
        _idleStart: 3686,
        _onTimeout: [Function (anonymous)],
        _timerArgs: undefined,
        _repeat: 3600000,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(kHasPrimitive)]: false,
        [Symbol(asyncId)]: 8,
        [Symbol(triggerId)]: 1
      },
      users: null,
      voiceStates: null
    }
  },

Under rest.requestManager there also looked to be handlerTimer which had an idleTimeout value of one hour.

      handlerTimer: Timeout {
        _idleTimeout: 3600000,
        _idlePrev: [Timeout],
        _idleNext: [TimersList],
        _idleStart: 3393,
        _onTimeout: [Function (anonymous)],
        _timerArgs: undefined,
        _repeat: 3600000,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(kHasPrimitive)]: false,
        [Symbol(asyncId)]: 7,
        [Symbol(triggerId)]: 1
      },

When my pinging bot disconnected, on replit this is what I saw in the console:

Preparing to connect to the gateway...
> DEBUG: Guilds we are connected to listed below
(end guild output)

> DEBUG: options => shardCount 1
> DEBUG: presence => status online
client.user is null (bot is not logged in).

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.

[WS => Manager] Fetched Gateway Information
    URL: wss://gateway.discord.gg
    Recommended Shards: 1

[WS => Manager] Session Limit Information
    Total: 1000
    Remaining: 997

[WS => Manager] Spawning shards: 0

[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 123ms

[WS => Shard 0] Clearing the HELLO timeout.

[WS => Shard 0] Setting a heartbeat interval for 41250ms.

[WS => Shard 0] [IDENTIFY] Shard 0/1 with intents: 33283

[WS => Shard 0] [READY] Session <hash>.

[WS => Shard 0] [ReadyHeartbeat] Sending a heartbeat.

[WS => Shard 0] Shard received all its guilds. Marking as fully ready.

===
Logged in as <PingingBot>#0000!
===

[WS => Shard 0] Heartbeat acknowledged, latency of 34ms.

[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.

[WS => Shard 0] Heartbeat acknowledged, latency of 25ms.

Note: WS is WebSocketManager https://discord.js.org/#/docs/discord.js/main/class/WebSocketManager https://discord.js.org/#/docs/discord.js/main/class/WebSocketShard

  ws: WebSocketManager {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    gateway: 'wss://gateway.discord.gg/',
    totalShards: 1,
    shards: Collection(1) [Map] { 0 => [WebSocketShard] },
    status: 3,
    destroyed: false,
    reconnecting: false,
    [Symbol(kCapture)]: false
  },

Findings in Thread

Some other findings in the this thread:

  • @zLupa mentioned discordjs 13.8.1 is not showing these issues.
  • The person in the OP was using discord.js@14.1.2
  • @djipgen reported v13.10.3 has the same problem.

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

5reactions
NeonWizardcommented, Oct 26, 2022

Update: My bot is going zombie mode about every 1-2 hours now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My bot has been offline with no error, it was working yesterday
This was working great yesterday, earlier it was online yet not responding to commands. I am hosting this in Replit. Am I doing...
Read more >
Why does my Discord bot say it's offline? - Quora
Go to a random server, or a DM (Direct Message). Then say something, like so.
Read more >
Why is my discord bot randomly go offline? - DigitalOcean
It's possible your Droplet gets too much worked up and kills your bot automatically in order to avoid any issues. What I'll suggest...
Read more >
Discord Bot build constantly crashing on Heroku-discord.js
That happens because you're using a web dyno: a dyno that is put asleep if the application doesn't serve a website for more...
Read more >
How to fix a Windows 10 boot loop | TechTarget
Go to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlCrashControl, and either create or edit a DWORD named AutoReboot and set it to 0. The blue ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found