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

Commit 3012985

Browse files
committed
remove socket error handler after request is done
`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.
1 parent 0b595c4 commit 3012985

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/request.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ function regRequest (uri, params, cb_) {
9292
}
9393

9494
function makeRequest (uri, params, cb_) {
95-
var cb = once(cb_)
95+
var socket
96+
var cb = once(function (er, parsed, resp, data) {
97+
if (socket) {
98+
// The socket might be returned to a pool for re-use, so don’t keep
99+
// the 'error' from here attached.
100+
socket.removeListener('error', cb)
101+
}
102+
103+
return cb_(er, parsed, resp, data)
104+
})
96105

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

151160
req.on('error', cb)
152161
req.on('socket', function (s) {
153-
s.on('error', cb)
162+
socket = s
163+
socket.on('error', cb)
154164
})
155165

156166
if (params.body && (params.body instanceof Stream)) {

0 commit comments

Comments
 (0)