DanielS
DanielS

Reputation: 555

Debugging an infinite loop in node.js

I seem to have an infinite loop somewhere in my node.js program. CPU goes up to 99.9% and the server just freezes.

Is there any way to break when the server freezes and then check the call stack to see what function is causing this?

Upvotes: 6

Views: 4339

Answers (3)

a sad dude
a sad dude

Reputation: 2825

If node-inspector doesn't work for you for some reason (it doesn't for me), but you can easily reproduce the bug, try running node --prof script, wait until it freezes and then a few more minutes, then just run node-tick-processor and see which function has the most ticks.

Upvotes: 1

DanielS
DanielS

Reputation: 555

So, I figured out a solution.

I installed node-inspector (awesome piece of work BTW) and compiled node in debug mode. Don't forget to activate it: node-inspector & will run it in the background.

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

node --debug script.js

The tricky part was getting the infinite loop to reappear, but after 20 minutes or so I lucked out and it did. I used the inspector's interface to pause the program (pause button on the right hand side) and then check out where the stack is currently is. Sometimes the pause will occur in native code but you can pause and resume it until you go back to the JavaScript.

Success :)

Upvotes: 4

Bas Slagter
Bas Slagter

Reputation: 9929

No, I don't think this is possible. Of course, you can add breakpoints but you must place them at points where you suspect where the infinite loop is running...after that, you can manually check what is happening. Also, you can add some line of code that is writing something to the screen or a textsfile as it hits the line. If you see some repeating line, you know where to look.

Upvotes: 0

Related Questions