lua
lua
DIAdiscord.js - Imagine an app
Created by lua on 9/30/2024 in #djs-questions
Node.js host process becomes "locked up" / "clogged" for simple bot
I have developed a simple bot that: 1. Detects when a message could possibly contain media/embeds 2. Reacts with up/down arrows, an "upvote bot" And it works great! It's very funny and useful for my Discord community named "MNSND". However, after a significant amount of time (anywhere from 1 to 24 hours), the bot becomes unresponsive and starts missing messages. I do not believe this to be a Discord.js issue directly, but possibly an issue with my code and how it interacts with Discord.js and the Node.js runtime. It's really weird! Here is the exact current source code and package.json: https://gist.github.com/luavixen/bb8f52535a0f8c8a958bdc41773b642d To be specific, it seems like the Node.js event loop is getting "clogged" or "locked up"? After a significant amount of time, the Node.js process fails to even perform basic asynchronous actions. After even longer, I cannot even terminate the process, I must send a SIGKILL signal. For example, I tried introducing a setTimeout call on program start that, after four hours, prints a message and calls process.exit. This would only ever complete 50% of the time on my development machine! I have reproduced this issue on: 1. Development machine - Windows 11 Laptop - Node.js v20.17.0 2. My personal server - Ubuntu LTS 20.04, hosted on my own hardware - Node.js v18.12.0 3. DigitalOcean droplet - Ubuntu LTS latest - Node.js Docker node:20-alpine To try and mitigate this issue, I put the bot inside of docker:
mnsnd-bot:
image: 'node:20-alpine'
container_name: 'mnsnd-upvote-bot'
pull_policy: always
restart: unless-stopped
network_mode: host
user: '1000:1000'
volumes:
- '/home/lua/apps/mnsnd-upvote-bot:/data'
working_dir: '/data'
command: 'node --enable-source-maps ./index.js'
mnsnd-bot:
image: 'node:20-alpine'
container_name: 'mnsnd-upvote-bot'
pull_policy: always
restart: unless-stopped
network_mode: host
user: '1000:1000'
volumes:
- '/home/lua/apps/mnsnd-upvote-bot:/data'
working_dir: '/data'
command: 'node --enable-source-maps ./index.js'
And created this cronjob:
0 */2 * * * /usr/bin/docker restart mnsnd-upvote-bot >/dev/null 2>&1
0 */2 * * * /usr/bin/docker restart mnsnd-upvote-bot >/dev/null 2>&1
Even still, the bot occasionally locks up and misses messages. It is completely unpredictable. Sometimes, Docker can simply ask the bot to gracefully shut down:
...
2024-09-29T23:58:03.421465376Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 76ms.
2024-09-29T23:58:44.700408912Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 75ms.
2024-09-29T23:59:25.986783539Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 75ms.
(Docker sends SIGTERM)
2024-09-30T00:00:02.081485472Z [mnsnd dbg] Received SIGTERM signal
2024-09-30T00:00:02.082989322Z [mnsnd dbg] Shutting down...
2024-09-30T00:00:02.111893643Z [discord.js dbg] [WS => Manager] Manager was destroyed:
(...)
2024-09-30T00:00:02.199721062Z [node.js dbg] Exiting with code 0...
(Process exits)
(Process created)
2024-09-30T00:00:03.562466635Z [mnsnd dbg] Starting up...
2024-09-30T00:00:03.616097539Z [discord.js dbg] Provided token: MTI1MzE1Mzc5MDE5MDY4MjEyMg.GekIOf.**************************************
2024-09-30T00:00:03.616158268Z [discord.js dbg] Preparing to connect to the gateway...
...
...
2024-09-29T23:58:03.421465376Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 76ms.
2024-09-29T23:58:44.700408912Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 75ms.
2024-09-29T23:59:25.986783539Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 75ms.
(Docker sends SIGTERM)
2024-09-30T00:00:02.081485472Z [mnsnd dbg] Received SIGTERM signal
2024-09-30T00:00:02.082989322Z [mnsnd dbg] Shutting down...
2024-09-30T00:00:02.111893643Z [discord.js dbg] [WS => Manager] Manager was destroyed:
(...)
2024-09-30T00:00:02.199721062Z [node.js dbg] Exiting with code 0...
(Process exits)
(Process created)
2024-09-30T00:00:03.562466635Z [mnsnd dbg] Starting up...
2024-09-30T00:00:03.616097539Z [discord.js dbg] Provided token: MTI1MzE1Mzc5MDE5MDY4MjEyMg.GekIOf.**************************************
2024-09-30T00:00:03.616158268Z [discord.js dbg] Preparing to connect to the gateway...
...
But sometimes, after the bot locks up, Docker must send a SIGKILL:
...
2024-09-28T16:48:23.864248801Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 86ms.
2024-09-28T16:49:05.148599771Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 77ms.
2024-09-28T16:49:09.821037631Z [mnsnd dbg] Msg @swing_racc "`//WHAT GAME I NEEDA KN0W//`" (skipped)
2024-09-28T16:49:13.539030409Z [mnsnd dbg] Msg @swing_racc ":000" (skipped)
(Docker sends SIGTERM)
(Docker sends SIGKILL 10s later)
(Process exits)
(Process created)
2024-09-28T18:00:12.647975190Z [mnsnd dbg] Starting up...
2024-09-28T18:00:12.706542854Z [discord.js dbg] Provided token: MTI1MzE1Mzc5MDE5MDY4MjEyMg.GekIOf.**************************************
2024-09-28T18:00:12.706720060Z [discord.js dbg] Preparing to connect to the gateway...
...
...
2024-09-28T16:48:23.864248801Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 86ms.
2024-09-28T16:49:05.148599771Z [discord.js dbg] [WS => Shard 0] Heartbeat acknowledged, latency of 77ms.
2024-09-28T16:49:09.821037631Z [mnsnd dbg] Msg @swing_racc "`//WHAT GAME I NEEDA KN0W//`" (skipped)
2024-09-28T16:49:13.539030409Z [mnsnd dbg] Msg @swing_racc ":000" (skipped)
(Docker sends SIGTERM)
(Docker sends SIGKILL 10s later)
(Process exits)
(Process created)
2024-09-28T18:00:12.647975190Z [mnsnd dbg] Starting up...
2024-09-28T18:00:12.706542854Z [discord.js dbg] Provided token: MTI1MzE1Mzc5MDE5MDY4MjEyMg.GekIOf.**************************************
2024-09-28T18:00:12.706720060Z [discord.js dbg] Preparing to connect to the gateway...
...
I have been working with Node.js for years and years, and I am kinda stumped. Help!
15 replies