Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch ECONNRESET? #705

Closed
xl-yang opened this issue Jul 3, 2017 · 13 comments
Closed

How to catch ECONNRESET? #705

xl-yang opened this issue Jul 3, 2017 · 13 comments

Comments

@xl-yang
Copy link

xl-yang commented Jul 3, 2017

  • Node.js Version: 7.10
  • OS: win32
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): https

I use https.request to read from server, and the response is fine. But after several minutes, ECONNRESET error crash the application. My question is, besides process.on('uncaughtException') (not recommended by Nodejs), is there a proper way to catch and handle ECONNRESET for https.request?

{ Error: read ECONNRESET at exports._errnoException (util.js:1050:11) at TCP.onread (net.js:582:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

Below is my code.
` var options = {
method: 'GET',
hostname: hostname,
path: url,
auth: username + ':' + password,
ca: [fs.readFileSync(path.resolve(__dirname, '../common/cer.cer'))],
}

const req = https.request(options, function (res) {

  res.on('data', (resdata) => {
    console.log('data');
  });

  res.on('end', () => {
    console.log('end');
  });
})

req.on('error', (e) => {
  console.log(e);
});

req.end();
@bnoordhuis
Copy link
Member

req.on('error', ...) is the right way to do it. Your example looks correct to me.

@xl-yang
Copy link
Author

xl-yang commented Jul 3, 2017

@bnoordhuis
Thanks for the reply. But sadly req.on('error', ...) doesn't get triggered. The request is fine and get correct resonse from server . The ECONNRESET happened several minutes later, so I guess by then the request is over and its event can't be triggered. Possibly I'm facing similar problems as nodejs/node#3595

BTW, I tried using with or without keep-alive agent, and even listened socket event 'error'/'close' when req.on('socket') and did socket.close() and socket.emit('agentRemove'). But none of this helps, the error occured over and over again.

Please suggest.

@bnoordhuis
Copy link
Member

Does it happen with the latest node.js 8? What you describe sounds like a bug that was fixed some time ago.

@xl-yang
Copy link
Author

xl-yang commented Jul 3, 2017

@bnoordhuis
I see this issue in docker(Linux) with Node 6.x and my local Windows 7 with Node 7.10.
I'll try Node 8 and let you know the result.

@xl-yang xl-yang closed this as completed Jul 3, 2017
@xl-yang
Copy link
Author

xl-yang commented Jul 3, 2017

Close by mistake

@xl-yang xl-yang reopened this Jul 3, 2017
@xl-yang
Copy link
Author

xl-yang commented Jul 3, 2017

@bnoordhuis I tried NodeJS 8, and got error again.
current time: 22:51:2.750 http.request complete
current time: 23:6:1.770 Error: read ECONNRESET
C:\nodejs>node -v
v8.1.3
`

@xl-yang
Copy link
Author

xl-yang commented Jul 5, 2017

@bnoordhuis please suggest if I need to open an issue in nodejs/node.

@bnoordhuis
Copy link
Member

Do you get 'error' and 'end' events? If yes, which one comes first? If no, file a bug but please include a ready-to-run test case.

@xl-yang
Copy link
Author

xl-yang commented Jul 5, 2017

@bnoordhuis I added event for res.on('end') and req.on('error'). 'end' got triggered, but 'error' never get triggered as I mentioned above. I'll fire a bug but not sure if I can find a proper URL to reappear this issue as the current URL is in my Intranet.

@josephrocca
Copy link

josephrocca commented Jul 7, 2017

I'm experiencing something similar (see my comment in this issue). This may also be related to this issue. I've posted code that reproduces the error (albeit, after a 5 or 10 minute wait) in the linked comment above.

Edit: Oh, just realised that this isn't the request repo! I think the issue may have the same source in any case.

@xl-yang
Copy link
Author

xl-yang commented Jul 7, 2017

nodejs/node#14102
I created issue under nodejs/node/issues, will use that to track this problem. Therefore close this. @josephrocca thanks for reproducing this error.

@xl-yang xl-yang closed this as completed Jul 7, 2017
@ghost
Copy link

ghost commented Jan 11, 2021

I used this block of code and for now it seems to work,

process.on('uncaughtException', (err) => console.log('node js process error\n', err));
const server = require('http2').createSecureServer(options, (req, res) => {
    // my codes...
});
server.on('clientError', (err, socket) => {
    if (err.code === 'ECONNRESET' || !socket.writable) socket.end('HTTP/2 400 Bad Request\n');
    console.log('client error\n', err);
});
server.listen(PORT, (err) => (!err) ? console.log('listening on port', PORT) : console.log('something went wrong\n', err));

@sam-github-sam
Copy link

ashtro-0 you're a life saver.
I just used the server.on lump and it caught the exception which originated in the node process I was trying to talk to
Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants