How to debug a socket hang up error in NodeJS?

node.jsSocketsHttpDebugging

node.js Problem Overview


I am getting the following error:

events.js:48
        throw arguments[1]; // Unhandled 'error' event
        ^
Error: socket hang up
    at createHangUpError (http.js:1091:15)
    at Socket.onend (http.js:1154:27)
    at TCP.onread (net.js:363:26)

In node v0.6.6, my code has multiple http.request and .get calls. Please suggest ways to track what causes the socket hang up, and on which request/call it is. Thank you

node.js Solutions


Solution 1 - node.js

Quick and dirty solution for development:

Use longjohn, you get long stack traces that will contain the async operations.

Clean and correct solution: Technically, in node, whenever you emit an 'error' event and no one listens to it, it will throw. To make it not throw, put a listener on it and handle it yourself. That way you can log the error with more information.

To have one listener for a group of calls you can use domains and also catch other errors on runtime. Make sure each async operation related to http(Server/Client) is in different domain context comparing to the other parts of the code, the domain will automatically listen to the error events and will propagate it to it's own handler. So you only listen to that handler and get the error data. You also get more information for free.(Domains are depreceated).

As Mike suggested you can also set NODE_DEBUG=net or use strace. They both provide you what is node doing internally.

Solution 2 - node.js

Additionally, you can set the NODE_DEBUG environment variable to net to get information about what all the sockets are doing. This way you can isolate which remote resource is resetting the connection.

Solution 3 - node.js

In addition to ftft1885's answer

http.get(url, function(res)
{
    var bodyChunks = [];

    res.on('data', function(chunk)
    {
        // Store data chunks in an array
        bodyChunks.push(chunk);
    }).on('error', function(e)
    {
        // Call callback function with the error object which comes from the response
    	callback(e, null);
    }).on('end', function()
    {
        // Call callback function with the concatenated chunks parsed as a JSON object (for example)
        callback(null, JSON.parse(Buffer.concat(bodyChunks)));
    });
}).on('error', function(e) {
    // Call callback function with the error object which comes from the request
	callback(e, null);
});

When I had this "socket hang up" error, it was because I wasn't catching the requests errors.

The callback function could be anything; it all depends on the needs of your application. Here's an exemple of a callback logging data with console.log and logging errors with console.error:

function callback(error, data) {
    if (error) {
        console.error('Something went wrong!');
        console.error(error);
    }
    else {
        console.log('All went fine.');
        console.log(data);
    }
}

Solution 4 - node.js

use

req.on('error',function(err){})

Solution 5 - node.js

Most probably your server socket connection was somehow closed before all http.ServerResponse objects have ended. Make sure that you have stopped all incoming requests before doing something with incoming connections (incomming connection is something different than incoming HTTP request).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser971956View Question on Stackoverflow
Solution 1 - node.jsFarid Nouri NeshatView Answer on Stackoverflow
Solution 2 - node.jsMikeView Answer on Stackoverflow
Solution 3 - node.jsfwoelffelView Answer on Stackoverflow
Solution 4 - node.jsftft1885View Answer on Stackoverflow
Solution 5 - node.jsMateusz CharytoniukView Answer on Stackoverflow