NodeJS - What does socket hang up actually mean

0 votes

I'm working on a web scraper with Node and Cheerio, and I'm receiving the following problem for a certain website (it only happens on this one page, not the others I try to scrape).

It happens at a different spot each time, so sometimes it's url x that causes the problem, and other times it's a whole different url:

  Error!: Error: socket hang up using [insert random URL, it's different every time]

Error: socket hang up
    at createHangUpError (http.js:1445:15)
    at Socket.socketOnEnd [as onend] (http.js:1541:23)
    at Socket.g (events.js:175:14)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

This is quite difficult to troubleshoot, and I'm not sure where to begin. What is a socket hang up error, first and foremost? Is it a 404 or similar error? Is it simply a case of the server refusing to connect?

I can't seem to find an explanation for this

EDIT: Here's an example of code that (sometimes) throws an error:

function scrapeNexts(url, oncomplete) {
    request(url, function(err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        $ = cheerio.load(body);
        // do stuff with the '$' cheerio content here
    });
}

There is no direct call to terminate the connection, but I'm using Node Request, which (as far as I can tell) uses http.get, so this isn't necessary; correct me if I'm mistaken!

EDIT 2: Here's a real-world example of code that's causing issues. The other variables, such as prodURL, are largely jquery selectors that were previously defined. This makes use of Node's async library.

function scrapeNexts(url, oncomplete) {
    request(url, function (err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        async.series([
                function (callback) {
                    $ = cheerio.load(body);
                    callback();
                },
                function (callback) {
                    $(prodURL).each(function () {
                        var theHref = $(this).attr('href');
                        urls.push(baseURL + theHref);
                    });
                    var next = $(next_select).first().attr('href');
                    oncomplete(next);
                }
            ]);
    });
}
Jun 7, 2022 in Node-js by Vaani
• 7,020 points
14,894 views

1 answer to this question.

0 votes

When a socket hang up is thrown, one of two things happens:

When you're a customer,
When you send a request to a distant server as a client and don't get a response in a timely manner. This error is caused by the end of your socket. You should catch this error and decide what to do with it, such as retrying the request or queueing it for later.

If you're a server or proxy,
When you, as a server, possibly a proxy server, get a request from a client and begin acting on it (or relaying the request to the upstream server), the client decides to cancel/abort the request before you have finished preparing the response.

When a customer cancels a request, this stack trace depicts what happened.

Trace: { [Error: socket hang up] code: 'ECONNRESET' }
    at ClientRequest.proxyError (your_server_code_error_handler.js:137:15)
    at ClientRequest.emit (events.js:117:20)
    at Socket.socketCloseListener (http.js:1526:9)
    at Socket.emit (events.js:95:17)
    at TCP.close (net.js:465:12)

Line http.js:1526:9points to the same socketCloseListener mentioned by @Blender, particularly:

// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.emit('error', createHangUpError());

...

function createHangUpError() {
  var error = new Error('socket hang up');
  error.code = 'ECONNRESET';
  return error;
}

If the client is a browser user, this is a common scenario. When a request for a resource/page takes a long time to load, visitors simply refresh the page. As a result of this action, the previous request is cancelled, resulting in this error on your server.

Because this issue is the result of a client's request, they should not expect to receive an error notice. As a result, there's no reason to consider this error to be important. Simply disregard it. This is bolstered by the fact that on such an error, the res socket that your client was listening to is destroyed, despite the fact that it is still editable.

console.log(res.socket.destroyed); //true

So, no point to send anything, except explicitly closing the response object:

res.end();

However, if you are a proxy server that has already transmitted the request to the upstream, you should abort your internal request to the upstream, signalling that you are uninterested in the response, which will alert the upstream server to maybe halt an expensive activity.

answered Jun 7, 2022 by Neha
• 8,940 points

Related Questions In Node-js

0 votes
1 answer

How to disconnect from tcp socket in NodeJs?

Hello, Try this out: net.createConnection() returns a Socket object. client.destroy() is what ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
2,404 views
0 votes
1 answer

Compress image up to maximum size(100kb) at NodeJS or React Native

You can follow this link : https://www.npmjs.com/package/image-conversion  This will ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 8,940 points
2,555 views
0 votes
1 answer

What version of npm works with nodejs 0.10?

Here's the complete list (between v0.10.0 - ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 8,940 points
468 views
0 votes
1 answer

How can i export socket.io into other modules in nodejs?

Because app.js is usually the main initialization ...READ MORE

answered Jun 17, 2022 in Node-js by Neha
• 8,940 points
1,495 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,199 views
0 votes
1 answer
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
811 views
0 votes
1 answer

How to host MEAN stack application with Angular and nodejs on windows IIS

It's fine that you're using Angular. Be ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 8,940 points
480 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 8,940 points
662 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP