RESTIFY: Error: socket hang up] code: 'ECONNRESET' on multiple requests

4.6k Views Asked by At

I am implementing a node app, which brings in order details from BigCommerce. Multiple calls are made to BigCommerce API asynchronously using Restify JsonClient.

It works fine for some calls but after that i gives error: [Error: socket hang up] code: 'ECONNRESET', sslError: undefined, body: {}

I have tried turning off socket pooling ie by setting agent=false, but it still gives same error.

Following is code which makes call to BigCommerce API

makeRequest = function (url, params, headers, orderDetails, cb) {
                var options = {
              headers: headers
              };

                var client = restify.createJsonClient({
                    url: url
                });

                client.get(options, function(err, req, res, obj) {
                    if(err){
                        console.log(err);
                        cb(err,obj);
                    } else if(obj != null) {
                        var result = obj;
                        if(orderDetails == null) {
                            cb(null,result);
                        } else {
                            cb(null, result , orderDetails);
                        }
                    }
                });
            };

I get following error:

{ [Error: socket hang up] code: 'ECONNRESET', sslError: unde
fined, body: {} } Error: socket hang up
    at SecurePair.error (tls.js:993:23)
    at EncryptedStream.CryptoStream._done (tls.js:689:22)
    at CleartextStream.read [as _read] (tls.js:490:24)
    at CleartextStream.Readable.read (_stream_readable.js:320:10)
    at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
    at EncryptedStream.g (events.js:175:14)
    at EncryptedStream.EventEmitter.emit (events.js:117:20)
    at finishMaybe (_stream_writable.js:352:12)
    at endWritable (_stream_writable.js:359:3)
    at EncryptedStream.Writable.end (_stream_writable.js:337:5)
    at EncryptedStream.CryptoStream.end (tls.js:628:31)
    at Socket.onend (_stream_readable.js:483:10) 

Why am i getting such error? How can i handle it?

Thanks

2

There are 2 best solutions below

0
On

Maybe you are running into this issue https://github.com/joyent/node/issues/5360

TL;DR: You could try with latest node version and secureOptions: constants.SSL_OP_NO_TLSv1_2 added to your options.

0
On

I just wanted to make sure that you're setting the agent setting in the right area.

Include the

"agent": false

in your options. (It's not set in the options in the code you pasted)

Per gfpacheco in the comments here: https://github.com/restify/node-restify/issues/485

By default NodeJS uses agents to keep the TCP connection open, so you can reuse it. The problem is that if the server is closed, or it closes your connection for whatever reason you get the ECONNRESET error.

To close the connection every time you just need to set agent: false in your client creation

I've tried this solution and it worked for me.

Other than that, the

"secureOptions": "constants.SSL_OP_NO_TLSv1_2"

solution posted here sounds like it could be the right path, since you're getting an sslError.