nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.8k stars 29.71k forks source link

ECONNRESET after response #27916

Closed ronag closed 1 year ago

ronag commented 5 years ago

This was for me a bit of surprising behavior. You can get a connection error after a response.

const http = require('http')

const server = http.createServer(function (req, res) {
  req.on('data', () => {})
  setTimeout(() => {
    // Prematurely ending the request. Usually due to a 5xx error.
    res.statusCode = 200
    res.end()
  }, 1000)
})

server.listen(0, function () {
  const req = http.request({
    port: this.address().port,
    method: 'POST',
    path: '/'
  })
  req.on('response', res => {
    console.log("!", res.statusCode)
    clearInterval(interval)
  })
  const interval = setInterval(() => {
    req.write(Buffer.alloc(32))
  }, 1000)
})

Which sometimes prints:

! 200
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
    at Socket.socketErrorListener (_http_client.js:391:9)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

I can't quite decide if this is wrong or right. I feel like it should either be a response or an error, not both... at least it should be consistent and happen just sometimes... Anyone, got any input?

niftylettuce commented 4 years ago

After deploying the previous fix, the number of occurrences went down drastically. I also just deployed this https://github.com/forwardemail/forwardemail.net/commit/f931682688d4c38c50568fbccfd433ba209f4ce9

whack-a-mole

niftylettuce commented 4 years ago

I have yet to see the issue since I have deployed the previous comment, but I will follow up if so.

niftylettuce commented 4 years ago

Issue came back up, none of the above comments were fixes. Please ignore.

MarcGodard commented 4 years ago

So I recently started seeing this in my console (terminal and server logs) after a switch to firefox browser, and noticed that it happens a lot when the tools are closed or disable cache is disabled. With cache disabled, I don't seem to have an issue. Sorry if this is the wrong venue for this question, but how do I debug this?

I also get this accompanying error:

Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:154:25)
    at writevGeneric (internal/stream_base_commons.js:137:3)
    at Socket._writeGeneric (net.js:784:11)
    at Socket._writev (net.js:793:8)
    at doWrite (_stream_writable.js:401:12)
    at clearBuffer (_stream_writable.js:519:5)
    at Socket.Writable.uncork (_stream_writable.js:338:7)
    at ServerResponse.OutgoingMessage.uncork (_http_outgoing.js:244:17)
    at connectionCorkNT (_http_outgoing.js:690:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'EPIPE',
  code: 'EPIPE',
  syscall: 'write'
}
vincenzo-mazzotta commented 3 years ago

Somebody has news about this bug? We have this problem in GCP with CloudRun nodejs app on alpine and calling mongodb. Thanks a lot for your support.

mcollina commented 3 years ago

@ronag is this problem still there?

vincenzo-mazzotta commented 3 years ago

We have in a GCP cloud run project nodejs/mongodb with alpine

MarcGodard commented 3 years ago

I still get this with nodejs.

Nefsen402 commented 2 years ago

We're getting this exact ECONNRESET error on our actual nodejs production deployment. However, when trying to run the test script, I'm unable to reproduce on my archlinux dev box. Even if I upload the script to our Ubuntu 20.04 production server, I still can't reproduce there with the test script.

Nefsen402 commented 2 years ago

To elaborate on the previous comment, It doesn't actually look like we're running into the error in the context of a network socket. We're running into the error through two node processes doing IPC over file descriptors. If the child process fails in some way, sometimes the parent process will fail with the ECONNRESET. No useful stack traces are generated.

im6 commented 2 years ago

I am also seeing this error. strangely that it only happens on one of my js project, i see this constantly when i use webpack-dev-server i am using mac M1 version 12.1 node version 16.14

node:events:498
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:442:9)
    at TLSSocket.emit (node:events:520:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -54,
  code: 'ECONNRESET',
  syscall: 'read'
}
Anatanokami commented 2 years ago

I am also seeing this error. strangely that it only happens on one of my js project, i see this constantly when i use webpack-dev-server i am using mac M1 version 12.1 node version 16.14

node:events:498
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:442:9)
    at TLSSocket.emit (node:events:520:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -54,
  code: 'ECONNRESET',
  syscall: 'read'
}

+1 here -- exactly the same error when running webpack or webpack-dev-server. Both with 16.14.0 and 17.7.0.

Anatanokami commented 2 years ago

@im6 I was able to fix this on my end by removing the import-statement for the Google Font. Both in my repo as well as in the one you linked to this issue. Seems to be a problem with one of the style loaders rather than webpack or Node.

im6 commented 2 years ago

@im6 I was able to fix this on my end by removing the import-statement for the Google Font. Both in my repo as well as in the one you linked to this issue. Seems to be a problem with one of the style loaders rather than webpack or Node.

Yes you are right @Anatanokami . It is @import() function in .less file that broke the app and emit the error. Thanks for pointing out.

jvinhit commented 2 years ago

I am also seeing this error. Error: read ECONNRESET\n at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20

meznaric commented 2 years ago

Upgrading Node to 16.15 from 16.14 solved the issue for us.

Specifically it was happening on webpack start.

mcollina commented 2 years ago

@ronag wdyt?

jmealo commented 1 year ago

I encounter a ton of ECONNRESET on my M1 Mac that other developers can't replicate on x86. I think it may be highly timing depending and my machine is able to reproduce it easily.

bnoordhuis commented 1 year ago

I can't reproduce this with v19.x on macOS. This issue is almost 4 years old and the http module has gone through a lot of changes in those years. The original bug almost certainly has been fixed.

Me-too commenters: if you still hit this bug in the latest v18.x or v19.x, please file a new issue and include steps to reproduce.

If you're seeing it with v16.x or v14.x, you may be out of luck. Those release lines are in maintenance mode and near their end of life.

jmealo commented 1 year ago

@bnoordhuis I made the same assumption with the new HTTP implementation but this seems to one level lower (possibly socket related race condition having to do with event handlers). It's difficult to reproduce in isolation but easy to reproduce in established projects that I can't share the source code to. We should definitely get a new issue opened up even without reproducible steps so that folks don't go crazy trying to debug and can share/collaborate.

aryanbhatt-clvt commented 3 months ago

Is there a solution for this ? Because it happens on my windows