4

I am working on a node.js app, and occasionally it seems to freeze. I assume this is because the user code thread has frozen. It seems to happen after about 5 minutes of usage, but I have no idea why. Are there any tools that would let you know where it's become deadlocked? Apart from adding logging on every line.

Updated to add more information....

I've added some trace statements and have narrowed it down to the following code:

exports.addLocationToRoute = function(req, res) {
    console.log("27");
    console.log(req.body);

    var queryConfig = {
        text: "INSERT INTO route_locations (route_id, location_id, order_id) VALUES ($1, $2, $3);",
        values: [req.params.id, req.body.locationId, req.body.order]
    };

    pg.connect(conString, function(err, client) {
    console.log("28");
...

I see 27 output in trace, but not 28. Is there a way to see why it is frozen between those two points?

update 2:

I just tried to reproduce again and it's become frozen at a different point in the code, but at this point it is also calling

pg.connect(conString, function(err, client) {
dan
  • 1,525
  • 1
  • 17
  • 35

2 Answers2

4

I'm using JetBrains WebStorm IDE for my Javascript development. It has full Node support, meaning you can set breakpoints and trace your code. However, WebStorm isn't free. You may also look at an older topic on Node debugging: How do I debug Node.js applications?

There's an accepted answer about using Chrome Development Tools, but I'd rather followed more popular answer's way of using node-inspector for debugging.

Community
  • 1
  • 1
devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • Any JetBrains IDE should have the Node.js plugin.. I use IntelliJ and it does the same – j pimmel Mar 19 '12 at 21:31
  • So can you use that to pause once it has become frozen to find out where in the callstack it is? This doesn't work with the chrome dev tools – dan Mar 19 '12 at 21:31
  • Yes, you can pause, not sure about call stack though. If there are no callbacks to be called, pausing won't lead you anywhere. @j pimmel: yes, Idea is fine too, but it costs more. – devmiles.com Mar 19 '12 at 21:36
0

You probably want to put node into --debug mode and analyze it with node-inspector.

When the code locks on some place, you can pause the execution with the pause-button ( http://pix.am/LYZz/ ) and inspect the stack trace to find locks.

Gottox
  • 682
  • 6
  • 15
  • I have that set up, but how can you use it to find out where it's become locked? I don't want to add break points because it only happens once every about 5-10 minutes. Stepping through the code, it will take hours to reproduce. – dan Mar 19 '12 at 21:28
  • Tell it to pause on exceptions, instead. Based on your update, though, it looks like the underlying PostgreSQL server is rejecting your connection (too many at once?) Why are you opening new connections on every query, rather than reusing the same connection over-and-over? –  Mar 19 '12 at 21:38
  • surely you must have one connection per request otherwise you could have two requests using the same connection at the same time? – dan Mar 19 '12 at 22:57
  • Look at the [Evented API](https://github.com/brianc/node-postgres). You create a query and get a query object, then have a callback executed on the results coming in. A single connection with multiple queries (that are queued in the order they're provided to PSQL). This is better than hammering PSQL with closing and opening connections that are only going to stress the server and possibly run into bugs on the number of connections open and being closed at the same time (like I suspect you have here). –  Mar 19 '12 at 23:05
  • Hmm, I don't disagree with you, but the documentation says that the callback style is prefered over the evented api – dan Mar 19 '12 at 23:13
  • They can say it, but TCP handshakes can be costly, authentication (if any) on each connection is costly, and hammering a PSQL server in a way it wasn't really designed to handle (thousands of independent clients, rather than the usual one-client-per-db) aren't actions I'd recommend. I've never had a problem with PSQL or MongoDB using a single connection and event handlers. –  Mar 19 '12 at 23:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9065/discussion-between-dan-and-david-ellis) – dan Mar 19 '12 at 23:32