Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

HTTP Client req.abort() can cause stream error in 0.10.5+ #5695

Closed
brycebaril opened this issue Jun 14, 2013 · 1 comment
Closed

HTTP Client req.abort() can cause stream error in 0.10.5+ #5695

brycebaril opened this issue Jun 14, 2013 · 1 comment

Comments

@brycebaril
Copy link

Calling req.abort() on a very active HTTP request causes Error: stream.push() after EOF in versions 0.10.5+

In this case, a HTTP client that wants to forcefully limit the size of the content it will pull:

client.js

var http = require("http")

var MAX_SIZE = 1024

var req = http.get("http://localhost:1338", function (res) {
  var size = 0
  res.on("data", function (chunk) {
    size += chunk.length
    if (size > MAX_SIZE) {
      console.log("Stop the madness! (aborting)")
      res.removeAllListeners("data") // not required, but prevents a second run of this "data" listener
      req.abort()
    }
  })
})

console.log("Sending request")
req.end()

server.js

var http = require("http")

var s = http.createServer(function (req, res) {
  console.log("incoming")
  res.writeHead(200, {"Content-Type": "text/plain"}) 
  var count = 0
  for (var i = 0; i < 1000000; i++) {
    count++ 
    res.write("packet " + i + "\n")
  }   
  console.log("ending, wrote %s", count)
  res.end()
})  

s.listen(1338)

With 0.10.4 (and below) this works:

bryce@x220:~/flood$ node client.js 
Sending request
Stop the madness! (aborting)
bryce@x220:~/flood$

With 0.10.5 (and above) it throws an Error:

bryce@x220:~/flood$ node client.js 
Sending request
Stop the madness! (aborting)

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: stream.push() after EOF
    at readableAddChunk (_stream_readable.js:146:15)
    at IncomingMessage.Readable.push (_stream_readable.js:127:10)
    at HTTPParser.parserOnBody [as onBody] (http.js:140:22)
    at Socket.socketOnData [as ondata] (http.js:1553:20)
    at TCP.onread (net.js:524:27)
bryce@x220:~/flood$

However, if the server is slowed down (setTimeout with 1ms works, setImmediate causes the same error), it works fine; whether this is a real-world issue is unclear.

@bnoordhuis
Copy link
Member

Dup of #5439 but with test case?

/cc @isaacs

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

No branches or pull requests

2 participants