6

I have a node application that periodically spikes to 100% on my production server. I would like to be able to send a signal to the node process that will give me a stack trace so I can find where in the code the problem is. Is there any easy way to do this?

My server is ubuntu 14.04.2 and I'm running node 0.12.2

Dirk
  • 71
  • 1
  • 4

2 Answers2

1

I was able to get a stack trace by compiling my own version of node and v8: https://github.com/joyent/node/issues/25263

Dirk
  • 71
  • 1
  • 4
-1

There is a function called console.trace() that is going to help you accomplish what you are looking to do.

How to print a stack trace in Node.js?

Set an event handler on process to watch for SIG1 with the following code:

process.on('SIGUSR1', function(){   console.trace   });

Now, from a terminal, issue the following command to send Node the SIGUSR1 signal:

killall -10 node

I'm not sure if the stack trace will go back far enough for you to be useful, it might just show the callback on SIG1USR, not sure. But I think this will get you closer.

Community
  • 1
  • 1
Brian
  • 3,264
  • 4
  • 30
  • 43
  • Thanks for your response Brian. I believe that will just show a stack trace of the function it is in, meaning, the stack of the event handler for SIGUSR1, not the stack trace of the place where the infinite loop is – Dirk May 06 '15 at 21:00
  • I don't think the suggestion to use SIGUSR1 will help. On receiving the signal, the process will wait to finish what it is doing. The event loop must come up for air before handling the SIGUSR1, and if it is stuck doing something it will never get there. – George Simms May 06 '15 at 21:01
  • Also in my tests, when the program is in a long loop it doesn't handle the SIGUSR2 event until the loop has finished – Dirk May 06 '15 at 21:01
  • Right, that's what I saw in my tests George – Dirk May 06 '15 at 21:02
  • Here's the output for a trace inside a process.on('SIGUSR2') event handler: ```Trace at process. (worker.js:14:13) at process.emit (events.js:104:17) at Signal.wrap.onsignal (node.js:655:46) ``` – Dirk May 06 '15 at 21:10