Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
remove socket error handler after request is done
Browse files Browse the repository at this point in the history
`npm-registry-client` uses `request`, which in turn uses an HTTP agent
for reusing sockets, so the `error` handlers registered on it in
`npm-registry-client` just piled up and kept being attached over the
entire lifetime of the socket.

This patch seeks to fix this by removing the listener from the socket
once the callback is invoked, as keeping it around after that would
just be a memory leak.
  • Loading branch information
addaleax committed Aug 27, 2016
1 parent 0b595c4 commit 3012985
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/request.js
Expand Up @@ -92,7 +92,16 @@ function regRequest (uri, params, cb_) {
}

function makeRequest (uri, params, cb_) {
var cb = once(cb_)
var socket
var cb = once(function (er, parsed, resp, data) {
if (socket) {
// The socket might be returned to a pool for re-use, so don’t keep
// the 'error' from here attached.
socket.removeListener('error', cb)
}

return cb_(er, parsed, resp, data)
})

var parsed = url.parse(uri)
var headers = {}
Expand Down Expand Up @@ -150,7 +159,8 @@ function makeRequest (uri, params, cb_) {

req.on('error', cb)
req.on('socket', function (s) {
s.on('error', cb)
socket = s
socket.on('error', cb)
})

if (params.body && (params.body instanceof Stream)) {
Expand Down

0 comments on commit 3012985

Please sign in to comment.