[http] Setting content-length forces connection: 'keep-alive' even when agent === false #7150

grncdr opened this Issue Feb 19, 2014 · 4 comments


None yet

4 participants


I'm not 100% sure this is a bug (this line seems very intentional) but it caused a behaviour I wasn't expecting so I wanted to get some input from the core team.

Essentially, in my app I am making a POST request against another service, but I want to opt out of connection pooling, so I have code like this:

var http = require('http')

function sendData (buffer, callback) {
    method: 'POST',
    hostname: 'example.com',
    path: '/',
    agent: false,
    headers: { 'content-length': buffer.length }
  }, callback).end(buffer)

(this has been simplified, I can create a minimal test-case if needed)

The behaviour I observed was callback being called with an IncomingMessage object, the IncomingMessage emitting data and end events (e.g. pipe worked fine) but the process would never exit, because the sockets were being kept open due to the connection: keep-alive header. I worked around it by adding a connection: close header to my code, but it seems like this behaviour can't possibly be what anybody wants when using agent: false.

If this is expected behaviour, I could contribute a documentation update, if it isn't I could contribute a test-case and patch.


@grncdr I've tried the same with 0.11 master and it worked as expected. What's the size for the buffer ? more information on consumer?


I tracked this down to some bad request proxying code. I was (accidentally) copying a connection header from the client, so I was specifying both the connection: 'keep-alive' header and agent: false. So while this is clearly not a node bug, maybe it would be useful for agent: false to force a connection: close header?


Further information, the upstream service is OpenStack glance, and I still see the same behaviour when remove agent: false. That is, after a successful request/response cycle, I'm left with an open socket keeping my process alive. It seems as though node should close that socket (no more requests are queued). No idea why this only happens with Glance. Is it possible that it's doing something to keep the connection alive?

@trevnorris trevnorris added the http label Feb 22, 2014
Node.js Foundation member

Thanks for sharing this! I think the http module already appends either Connection: close or Connection: keep-alive, if one isn't overridden by user-supplied headers.

If you have a test case that contradicts this and shows that it is broken - please feel free to reopen this issue.

@indutny indutny closed this Mar 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment