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

Simple http proxy with socketPath leads to EPIPE error #40553

Closed
mhassan1 opened this issue Oct 21, 2021 · 5 comments
Closed

Simple http proxy with socketPath leads to EPIPE error #40553

mhassan1 opened this issue Oct 21, 2021 · 5 comments
Labels
http Issues or PRs related to the http subsystem.

Comments

@mhassan1
Copy link

mhassan1 commented Oct 21, 2021

Version

v16.7.0

Platform

Linux e332ae582690 5.10.47-linuxkit #1 SMP Sat Jul 3 21:51:47 UTC 2021 x86_64 Linux

Subsystem

No response

What steps will reproduce the bug?

I have created a simple HTTP proxy that pipes to a socket:

const http = require('http')
http.createServer((req, res) => {
  const proxyReq = http.request({
    host: null,
    port: null,
    socketPath: 'sock.sock',
    path: req.url,
    method: req.method,
    headers: req.headers
  }, (_res) => {
    res.writeHead(_res.statusCode, _res.headers)
    _res.pipe(res, { end: true })
  })
  req.pipe(proxyReq, { end: true })
}).listen(8080)

On v16.7.0, this leads to an EPIPE error after a few hundred requests. On v16.6.2, it does not hit an EPIPE error after many thousands of requests.

A full Docker Linux reproduction is here: https://github.com/mhassan1/node-16-7-0-proxy-epipe

How often does it reproduce? Is there a required condition?

It reproduces every time within a few hundred requests.

What is the expected behavior?

No EPIPE.

What do you see instead?

EPIPE after a few hundred requests:

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

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:164:15)
    at writeGeneric (node:internal/stream_base_commons:155:3)
    at Socket._writeGeneric (node:net:780:11)
    at Socket._write (node:net:792:8)
    at doWrite (node:internal/streams/writable:408:12)
    at clearBuffer (node:internal/streams/writable:569:7)
    at onwrite (node:internal/streams/writable:461:7)
    at afterWriteDispatched (node:internal/stream_base_commons:167:9)
    at writevGeneric (node:internal/stream_base_commons:147:3)
    at Socket._writeGeneric (node:net:778:11)
Emitted 'error' event on ClientRequest instance at:
    at Socket.socketErrorListener (node:_http_client:447:9)
    at Socket.emit (node:events:394: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: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Additional information

  • This is a simplified reproduction of the same issue I am seeing when using http-proxy-middleware
  • This does not reproduce on OS X
  • This does not reproduce when proxying to a port
  • This reproduces on v14.18.0 but not v14.17.6
  • This does not reproduce when the proxy and the backend are listening in the same Node process; they must be listening in different processes
@mhassan1
Copy link
Author

I have linked a few issues from other repositories that seem like they are related; for example: hapijs/hapi#4296.

@kanongil
Copy link
Contributor

Looking at the commits going into v14.18.0, I would say that libuv/libuv@ce15b84 is a likely culprit, as a revert of a previously reverted commit.

@mhassan1
Copy link
Author

mhassan1 commented Nov 3, 2021

I have been trying to debug this. Some things I've noticed:

  1. If I modify the proxy to use end: false (i.e. req.pipe(proxyReq, { end: false })), EPIPE never happens. If I call proxyReq.end() manually 1 second later, EPIPE does not happen.
  2. If I look at proxyReq.socket._writableState when EPIPE happens, it looks identical to the case where EPIPE does not happen. I can't tell what about the state of the ClientRequest is causing the EPIPE to be emitted.

@mhassan1
Copy link
Author

mhassan1 commented Dec 10, 2021

This should be resolved by #41116.

@saberph
Copy link

saberph commented Jan 19, 2022

Hi,

Do you know when it will be pushed to v14 LTS branch? We are affected by this too.

Thank you =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants