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:
And created this cronjob:
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:
But sometimes, after the bot locks up, Docker must send a SIGKILL:
I have been working with Node.js for years and years, and I am kinda stumped. Help!
Gist
Implementation of my Discord.js upvote/downvote bot for media/embeds.
Implementation of my Discord.js upvote/downvote bot for media/embeds. - index.ts
8 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPAnother example:
The only thing I can think of next is to launch it in a debugger, learn a bunch about Discord.js's internals, and watch it and wait for it to lock up again.
I know that it's not my issue, but thanks for bringing that up! @matt4499, I like the way you re-wrote the loop to make it more obvious and I'll copy that over into my codebase. That loop really just iterates over the results of the
pattern
regex and unless message.content
changes, it will always terminate.
Additionally, it never actually gets stuck in a loop! Both CPU and memory usage stay way down, and the function in question is entirely synchronous (unless there's something evil going on).👍
At least that's what I think! I am not /actively/ monitoring those two metrics.
Retracting my previous statement, huuuuh ??? Note the 25% usage of a 4 core system.
Apparently this was true at least in my testing, but checking on it in production it is seemingly getting stuck. I'm going to feel like such an idiot if this is the issue LOL.
I have rewritten the function as follows, just to be absolutely sure that this possible bug is gone:
I FORGOT THE
g
YOU'RE RIGHT
Ohhhh my god lol. Okay I'm gonna add that too