Debug endless loop in node.js

It seems I have an infinite loop somewhere in my node.js. program The CPU reaches 99.9%, and the server just freezes.

Is there a way to break when the server freezes and then check the call stack to find out which function is calling this?

+6
source share
3 answers

So, I understood the solution.

I installed node-inspector (the wonderful part of BTW) and compiled node in debug mode. Remember to activate it: node-inspector & will launch it in the background.

After that, I started the node process with the V8 debug flag:

node --debug script.js

The tricky part was getting an endless loop to reappear, but after 20 minutes I got lucky and it happened. I used the inspector interface to pause the program (pause button on the right) and then check where the stack is. Sometimes a pause will occur in the native code, but you can pause it and resume until you return to JavaScript.

Success:)

+4
source

If the node-inspector for some reason does not work (this is not for me), but you can easily reproduce the error, try running the node --prof script , wait until it hangs, and then a few more minutes, then just run node-tick-processor and see which function has the most ticks.

+1
source

No, I do not think this is possible. Of course, you can add breakpoints, but you must place them at the points where you suspect where the infinite loop is working ... after that you can manually check what happens. Alternatively, you can add some line of code that writes something to the screen or text file when it hits the line. If you see some kind of repeating line, you know where to look.

0
source

Source: https://habr.com/ru/post/896625/


All Articles