Skip to content

Commit ffe4904

Browse files
Aviv Bareljoeferner
Aviv Barel
authored andcommitted
Fix big memory leak
connectRequests were not properly freed, errors were not handled correctly. Since this is a duplex stream, the connection needs to be closed manually on error. sslSemaphores were not being deleted at all. Please notice that the deletion of semaphores can be done better.
1 parent 4119422 commit ffe4904

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/proxy.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ Proxy.prototype._onHttpServerConnectData = function(req, socket, head) {
382382
}
383383
return makeConnection(port);
384384
});
385+
delete self.sslSemaphores[wildcardHost];
385386
});
386387
} else {
387388
return makeConnection(this.httpPort);
@@ -394,18 +395,22 @@ Proxy.prototype._onHttpServerConnectData = function(req, socket, head) {
394395
allowHalfOpen: true
395396
}, function() {
396397
// create a tunnel between the two hosts
397-
conn.on('finish', () => {
398+
var connectKey = conn.localPort + ':' + conn.remotePort;
399+
self.connectRequests[connectKey] = req;
400+
const cleanupFunction = function() {
401+
delete self.connectRequests[connectKey];
402+
};
403+
conn.on('close', () => {
404+
cleanupFunction();
398405
socket.destroy();
399406
});
400407
socket.on('close', () => {
401-
conn.end();
408+
conn.destroy();
402409
});
403-
var connectKey = conn.localPort + ':' + conn.remotePort;
404-
self.connectRequests[connectKey] = req;
410+
conn.on('error', () => conn.destroy());
405411
socket.pipe(conn);
406412
conn.pipe(socket);
407413
socket.emit('data', head);
408-
conn.on('end', function() { delete self.connectRequests[connectKey]; });
409414
return socket.resume();
410415
});
411416
conn.on('error', self._onSocketError.bind(self, 'PROXY_TO_PROXY_SOCKET'));

0 commit comments

Comments
 (0)