node js cpu 100%

前端 未结 5 2016
花落未央
花落未央 2020-12-24 13:13

We\'re having a problem where every once in a while one of our environments our node app runs on 100% CPU. The server isn\'t very active and usually runs on 0%-2% CPU. I was

5条回答
  •  余生分开走
    2020-12-24 13:53

    Constantly running at 100% CPU is typical for infinite loop. This is a real problem in singlethreaded nodejs but unfortunately there is a lack of info on it.

    Eventually I have found the only useful article: How to track the deadloop in nodejs:

    Connect to you server via SSH. Identify nodejs process id (let it be 4702, for example). Now, let’s tell the process to listen for debugging requests. Yes, we’re using a command called kill. No, we’re not killing the process. We’re sending it a different signal.

    kill -SIGUSR1 4702
    

    Once you do this, the process is open to a debugger connection. In fact, it will print a special URL to its console log, and you can open that URL in Chrome to debug the process! But, maybe you don’t want to drill a hole through a firewall and a container configuration just to make that connection. Yeah, me neither. So let’s debug at the command line instead:

    node inspect -p 4702
    

    You’ll see this prompt:

    debug>
    

    Then type:

    pause
    

    And you get back:

    break in file:///somewhere/something.js:555
    >555         for (prop in inputObject) {
    510             if (hasOwnProp(inputObject, prop)) {
    511                 normalizedProp = normalizeUnits(prop);
    

    Yes! We have our first hint. The app was executing line 555 in file something.js. That might be enough to see the bug right away. But usually we need more information than that. You can type backtrace to get a complete stack trace:

    #0 someFunctionName file:///somewhere/somefile.js:444:22
    #1 someFunctionName file:///somewhere/somefile.js:555:33
    #2 someFunctionName file:///somewhere/somefile.js:666:44
    

    … And so on.

提交回复
热议问题