Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a socket hang up error in NodeJS?

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

like image 335
user971956 Avatar asked May 30 '12 10:05

user971956


People also ask

What is error socket hang up?

You can receive "socket hang up" as a client when you're trying to make a second request to a Django's development web server over the same connection. It doesn't support keep-alive . And in case your client expects it does, you get the error.

How do I fix Econnreset error read?

I solved the problem by simply connecting to a different network. That is one of the possible problems. As discussed above, ECONNRESET means that the TCP conversation abruptly closed its end of the connection. Your internet connection might be blocking you from connecting to some servers.


2 Answers

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.

like image 146
Farid Nouri Neshat Avatar answered Sep 25 '22 09:09

Farid Nouri Neshat


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.

like image 27
Mike Avatar answered Sep 23 '22 09:09

Mike