14

And actually, I don't fully understand why my code is not in the stack trace, if node is single threaded. Maybe I'm fundamentally misunderstanding, something, but why does my application sometimes die with a stack trace that doesn't have anything I've written in it?

I'm writing a pretty simple proxy server using node/express. As an example, I was periodically getting this "socket hangup error":

Error: socket hang up
 at createHangUpError (_http_client.js:250:15)
 at Socket.socketOnEnd (_http_client.js:342:23)
 at emitNone (events.js:91:20)
 at Socket.emit (events.js:185:7)
 at endReadableNT (_stream_readable.js:926:12)
 at _combinedTickCallback (internal/process/next_tick.js:74:11)
 at process._tickCallback (internal/process/next_tick.js:98:9) code: 'ECONNRESET' }

And since none of the javascript files in the stack trace are mine, I had no idea where this was coming from. It was basically trial and error, trying to catch errors and adding .on style error-handlers until I found the right place.

I feel like I'm fundamentally missing something - what I should I be doing differently in order to debug errors like this? How do I know where to handle it if I can't see what (in my code) is causing it? How do I know whether I should be using a try/catch block, or something like request.on('error') {...}?

Jer
  • 4,958
  • 6
  • 28
  • 39
  • http://stackoverflow.com/search?q=express+socket+hang+up – Kevin B Jan 18 '17 at 20:39
  • You can try using something like [longjohn](https://www.npmjs.com/package/longjohn) which will attempt to maintain stack traces across async boundaries (`setTimeout`, `process.nextTick`, etc.). Although you should avoid using it in production as it will affect performance. – idbehold Jan 18 '17 at 20:42
  • Share the code please? – 1Mayur Jan 18 '17 at 21:01
  • Kindly refer to the fourth awnser http://stackoverflow.com/questions/16995184/nodejs-what-does-socket-hang-up-actually-mean Exactly what you need for the server/proxy case – Keval Gohil Jan 19 '17 at 05:42
  • To all the comments above, I don't think he's referring to the solution to socket.io specific case, but rather on **how to debug these sort of errors** on node (not library specific). – zurfyx Jan 25 '17 at 14:53

5 Answers5

12

Some errors, like the one mentioned by you, are not caused by your code. In fact it is caused by the absence of code in your application. (For example, your application code is probably missing the code for gracefully handling ECONNRESET i.e. remote socket disconnection.

Now, to your question about how to debug such errors (including third-party code). Of course, you can use stack-trace and longjohn etc.

But, for me, the easier & quicker solution is to run the application in debug mode with --inspect option, with Chrome debugger to inspect it (no breakpoints), with Pause on Exceptions option enabled. That's all you need to do. Now whenever there is an exception, chrome debugger will pause the application exactly at the line where the exception is thrown. Makes it a lot easier to find such bugs.

Pause On Exception

Hope this helps you!

Santanu Biswas
  • 4,286
  • 1
  • 21
  • 20
4

You could do something like for debugging such errors.

process.on('uncaughtException', function(err) {
  console.log(err.stack);
  throw err;
});

You could also increase your stack trace size limit and/or stack size.

node --stack_trace_limit=200 app.js //defaults to 10
node --stack-size=1024 app.js // defaults to 492kB
Kaushik Wavhal
  • 432
  • 6
  • 16
3

Contrary to your assumption, the single thread paradigm of node.js cause these types of error. In a multi-thread environment like java, all the called functions are executed inside the caller function:

java -> A -> B -> C -> D

So if the A() is wrapped in a try and catch all the internal exceptions are caught.

But in Async environment, the callback function is executed outside the caller function:

node -> A -> B(C)
node -> I -> C -> D

Here the function A call async function B (Usually a library) with callback function C as an argument, function B start an async task after it's finished node.js call function I which is an internal function of that library and it calls function C. Here you see I, C and D are called outside of your code.

So there are two things to consider here:

1- you must wrap your callback function code in try and catch.

2- there may be an exception in function I which you can not catch in your code. These are those exceptions that you are referring which none of the javascript files in the stack trace are yours because it's not initiated in your code.

Now if the function B (its library) is good written it must provide some means to catch those exceptions. One of them would be on('error', cb). So you should always check the library documentation to see how you can catch and handle such exceptions.

If the library is poorly written and nothing is provided to catch its exceptions in your code, you can use a debugger like chrome inspector or WebStorm debugger to catch them but there is no other way than manipulating its source code so at least they are bypassed to your code, or you can file a bug report.

Node.js also provide an uncaught exception handler which catches all uncaught exceptions:

process.on('uncaughtException', (err) => {
    process.exit(1);
});

although it's risky to continue execution after here because things could be in an uncertain state, but it's a good place to log errors.

Ali
  • 18,627
  • 13
  • 75
  • 90
2

Stack Trace does not contain your file because this type of error is created server connection issue like:

Host Timeout

[ 'code' ]
e.code => ECONNRESET

Console output

{ Error: socket hang up
    at createHangUpError (_http_client.js:250:15)
    at TLSSocket.socketCloseListener (_http_client.js:282:23)
    at emitOne (events.js:101:20)
    at TLSSocket.emit (events.js:188:7)
    at TCP._handle.close [as _onclose] (net.js:492:12) code: 'ECONNRESET' }

OR when the server terminates the connection unexpectedly or does not send a response.

For more info check node app error codes.

or refer to this issue.

Rohit Dhiman
  • 2,333
  • 12
  • 32
1

You might try the built in debugger for node.js. This debugger allows you to step through code that is not yours.

node debug script.js

Another good tool is node-inspector.

This article has some other debug options that could be useful in determining the cause of your exception: How do I debug Node.js applications?

Community
  • 1
  • 1
John Meyer
  • 1,938
  • 1
  • 24
  • 35