diff options
Diffstat (limited to 'lib/http.js')
-rw-r--r-- | lib/http.js | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/http.js b/lib/http.js index dacedd4477..1f4db7a647 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1635,8 +1635,10 @@ function parserOnIncomingClient(res, shouldKeepAlive) { COUNTER_HTTP_CLIENT_RESPONSE(); req.res = res; res.req = req; - var handled = req.emit('response', res); + + // add our listener first, so that we guarantee socket cleanup res.on('end', responseOnEnd); + var handled = req.emit('response', res); // If the user did not listen for the 'response' event, then they // can't possibly read the data, so we ._dump() it into the void @@ -1667,7 +1669,11 @@ function responseOnEnd() { } socket.removeListener('close', socketCloseListener); socket.removeListener('error', socketErrorListener); - socket.emit('free'); + // Mark this socket as available, AFTER user-added end + // handlers have a chance to run. + process.nextTick(function() { + socket.emit('free'); + }); } } |