2

How do I find out where that error is originated from ? The error output doesn't look very descriptive. From the log, it was ECONNRESET error.

process.on('uncaughtException', function(err) {

})
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • 1
    The best start would be to stop using `uncaughtException` handlers and properly handle all errors in your callbacks and promises. – Robert Rossmann Mar 29 '16 at 06:59
  • @RobertRossmann is there any other alternative for debugging? It usually happens after the app rests for a while. – Tuan Anh Tran Mar 29 '16 at 07:09
  • Maybe here is one useful link, [configure Node to dump core on an uncaught exception](http://www.joyent.com/developers/node/debug#postmortem) – zangw Mar 29 '16 at 07:10
  • 1
    @TuanAnhTran handling errors **is** debugging. Trust me, if you do not handle errors today, you should immediately start doing so. Otherwise your app will never be stable, it will always crash with the most unexpected reasons and you will have no clue where that came from. – Robert Rossmann Mar 29 '16 at 09:01
  • @RobertRossmann thank you. – Tuan Anh Tran Mar 29 '16 at 09:03

1 Answers1

0

I had this same problem.

What made it hard, is it's not in my code, but in a library i use.

So as you also have found out, the stack trace provided isn't helpful at all. Even after installing a package like longjohn your stack track from this server crash is all internal:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
---------------------------------------------
    at _destroy (internal/streams/destroy.js:34:15)
    at Socket._destroy (net.js:607:3)
    at Socket.destroy (internal/streams/destroy.js:32:8)
    at TCP.onStreamRead (internal/stream_base_commons.js:111:19)
Waiting for the debugger to disconnect...

You need to handle the emitter.on("error") events, as basically described here: Node js ECONNRESET

In my situation I was able to reproduce the error by sending an invalid password via a curl request. and was able to solve because the library I'm uses exposes the request object for incoming requests.

So with that, here's my solution code:

function onRequest(requestDetails){
            requestDetails.request.connection.on( "error", ( _err ) => {
                log.error( "request.connection.on.error", _err );
            } );

            let reqSocket = requestDetails.request.socket;
            reqSocket.on( "error", ( _err ) => {
                log.error( "request.socket.on.error", _err );
                reqSocket.destroy( _err );
            } );

            requestDetails.request.on( "error", ( _err ) => {
                log.error( "request.on.error", _err );
            } );

            // my actual request handling code goes here.....
}

I also added error handler for the server object, though that wasn't needed in this case:

    server.on( 'requestFailed', ( { request, error }: any ) => {
        log.error( `Request ${ request.url } failed`, error );
    } );

    server.on( 'error', ( ...args: any[] ) => {
        log.error( "server.on.error", args );
    } );

    server.on( 'connection', function ( socket ) {
        socket.on( 'error', function ( err ) {
            log.error( "Client socket error:", err );
            socket.destroy( err );
        } );
    } );

JasonS
  • 7,443
  • 5
  • 41
  • 61