简体   繁体   中英

Node.js (express.js) process hanging after few days under PM2

I have a test/staging setup where I am running 5 node processes with PM2 on a t2.small machine (1 core and 1.7 GB RAM). Of late I am seeing that one of the processes (which receives the maximum traffic) hangs. Since the process does not exit or throw an exception PM2 assumes it to be alive. As a result my health checks fail (request is hung and never returns) and the API is DOWN.

I am using nodejs with express framework. I am not making any remote network calls from my code which could be blocking it. Restart fixes the issue always.

I can think of a few hacks to restart the process but I want to find answers to a few questions:

  1. Is this a memory allocation issue ? Even when the process is hung none of the 5 processes take up more than 100 MB each. free -m returns a healthy 800-900 MB. My question is if a node process is not able to allocate memory would it throw OutOfMemory like java and other VM based languages or would it just hang ?
  2. What is the lifecycle of a call back ? ie when is it GCed. In my code sometimes I do not call the callback stack and directly return the response to the browser. Would it lead to unused/garbage callback since I bypassed them. For example method A-> method B(cb1) -> method C(cb2). Method C just has a return statement and does not call cb2. Would it lead to dangling cb1 and cb2 objects ? Could this lead to my issue of nodejs hanging ?
  3. I saw a few scripts to monitor hanging processes but is there a better way to handle this (process restart) until I find the root cause ?
  4. Is t2.small the culprit ? ie finite memory and CPU.

The issue was with one of the iOS push notification libraries. It was not getting initialized properly (missing attribute). Whenever i tried tried to call that lib in the code the whole JS VM would just freeze.

Dont know the reason why it froze but fixing the library initialization solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM