Node-http-proxy: Error emitted on proxy server after original request is aborted

Created on 9 Jun 2020  路  2Comments  路  Source: http-party/node-http-proxy

I found that for Node 10+ if a request is cancelled before being proxied, the proxyServer emits an error instead of an econnreset event. This does not happen for < Node 8. I believe this is due to a change in the lifetime of error event listeners on the original request socket.

I think the underlying issue is that in lib/http-proxy/passes/web-incoming.js there is an event listener on the aborted event on a request. aborted is a property, I believe the event should be abort. Making this change fixes the issue in my reproduction path.

https://github.com/http-party/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L146

Versions:

  • Node 12.16.3
  • http-proxy 1.17.0
  • express 4.17.1

Reproduction path:

  • Open server (source code below)
  • Open a request to the server (I used cURL, you could use your browser) and immediately stop the request

    • curl http://127.0.0.1:8080/echo

  • Observe the following error:
proxyRequest start
proxyRequest Error: socket hang up
    at connResetException (internal/errors.js:608:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:322:22)
    at TCP.<anonymous> (net.js:672:12) {
  code: 'ECONNRESET'
}

Server code:

var httpProxy = require('http-proxy'),
    express = require('express'),
    http = require('http'),
    httpServer,
    app 

function sleep(ms) {
  const date = Date.now()
  let currentDate
  while (!currentDate || currentDate - date < ms) currentDate = Date.now()
}       

function proxyRequest(request, response) {
  var proxy = httpProxy.createProxyServer({})

  console.log('proxyRequest start')

  proxy.on('proxyReq', (proxyReq, req, res, options) => {
    sleep(5000)
  })

  proxy.on('error', err => { 
    console.error('proxyRequest', err)
  })

  proxy.web(request, response, { target:'127.0.0.1/demo/api/product' })
}   

process.title="test"
app = express()
httpServer = http.createServer(app)

app.get('/echo', proxyRequest)

app.listen('8080') 

Most helpful comment

Is there any progress on this issue?

All 2 comments

There was another issue that referenced this change in Node 10 that I think _might_ be responsible, but I'm unsure. https://github.com/nodejs/node/pull/18868

Is there any progress on this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DominicTobias picture DominicTobias  路  3Comments

damianchojna picture damianchojna  路  5Comments

jnjnljljjsw picture jnjnljljjsw  路  6Comments

guillaume-at-palo picture guillaume-at-palo  路  4Comments

catamphetamine picture catamphetamine  路  6Comments