Next.js Discord

Discord Forum

Nextjs + PM2 keeps crashing and restarting

Unanswered
MacGillivray's Warbler posted this in #help-forum
Open in Discord
Avatar
MacGillivray's WarblerOP
Hi, unfortunately I'm not able to provide a lot of details right now because I don't think I even fully comprehend the issue yet, but essentially I'm running a bunch of Nextjs sites on FreeBSD and Linux servers. I'm using Node 20.10.0 and Next versions range from 13.x to 14.1.0. The Nextjs applications are made available through an Apache reverse proxy and orchestrated using pm2. My, unfortunately very vague, issue now is that they keep crashing and restarting. CPU and memory usage are not out of the ordinary (no growth, nothing is at allocated capacity for extended periods of time, system has many more resources), versions are all different, when crashes occur, there are no logs. Crashes seems to correlate with site usage (more frequently used sites crash more often, actively using an otherwise unused site will eventually lead to a crash), but I could not make out any particular trigger yet. The Nextjs instances last anywhere from 30 minutes to an hour before they crash and restart. This is highly problematic because it means downtime of a few seconds while the app is restarting. I know there aren't many details, but I'm crossing my fingers and hoping that there might be others facing the same or a similar issue and maybe being able to provide insights. Ideas or suggestions, other than deploying on Vercel, are also welcome. I can potentially provide more info for specific questions. If anyone else faces these issues, even without knowing why, feel free to share your experiences and describe how you're deploying your apps and any other noteworthy details that could help narrow the issue down. I have yet to do more thorough memory analysis or in-depth monitoring. Thanks for any and all help.

4 Replies

Avatar
Plott Hound
hi. yes it will be very hard to diagnose without seeing logs.

I'm running a similar setup using PM2 but with apache and reverse proxy mostly without issue. If many of them are building at the same time or i have a lot of concurrent users they can crash due to the RAM allocation.

If I were to guess - the issue is stemming from RAM or possibly some rate limiting from your provider. But this is unlikely since you have confirmed RAM and CPU are not an issue, but its worth noting Next.js instances will easily use more than 1Gb of ram per instance so double check your logging is reflecting what is happening accurately.

All i can suggest is that you provide any relevant logging and try A/B testing in different environments so we can pinpoint the issue
Avatar
Lionhead
@MacGillivray's Warbler did you find the solution? I am curious
Avatar
MacGillivray's WarblerOP
Not entirely sure to be honest. It's still a bit of an open issue, but I have been busy with other things. For sites with exceedingly high restarts, I'd suggest looking for bugs in your code that are causing endless loops, endless recursion or anything of the sort. I'll try figuring it out when I have more time and I'll keep you posted, if I find the issue, if you want.
Avatar
Lionhead
I don't have anything of the sort, just investigating