Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed the socket leak

  • Loading branch information...
commit 575fe1237f4a84619d092bc67a85bceda0ed84d3 1 parent 93d66c8
@3rd-Eden authored
Showing with 12 additions and 21 deletions.
  1. +11 −5 lib/connectionpool.js
  2. +1 −16 test/connectionpool.test.js
View
16 lib/connectionpool.js
@@ -296,12 +296,15 @@ Manager.prototype.free = function free(keep, hard) {
// default to 0 if no arguments are supplied
keep = +keep || 0;
- var saved = 0
- , self = this;
+ // create a back-up of the pool as we will be removing items from the array
+ // and this could cause memory / socket leaks as we are unable to close some
+ // connections in the array as the index has moved.
+ var pool = this.pool.slice(0)
+ , saved = 0;
- for (var i = 0, length = this.pool.length; i < length; i++) {
- var connection = this.pool[i];
- var probability = this.isAvailable(connection);
+ for (var i = 0, length = pool.length; i < length; i++) {
+ var connection = pool[i]
+ , probability = this.isAvailable(connection);
// this is still a healthy connection, so try we probably just want to keep it
if (keep && saved < keep && probability === 100) {
@@ -312,6 +315,9 @@ Manager.prototype.free = function free(keep, hard) {
this.release(connection, hard);
}
+ // clear the back-up
+ pool.length = 0;
+
// see how much connections are still available
this.emit('free', saved, this.pool.length);
};
View
17 test/connectionpool.test.js
@@ -204,22 +204,7 @@ describe('connectionpool', function () {
expect(saved).to.eql(0);
});
- pool.once('end', function () {
- var handles = process._getActiveHandles().filter(function (handle) {
- return handle instanceof net.Socket;
- });
-
- var active = [];
-
- handles.forEach(function (socket) {
- var has = backup.indexOf(socket);
- if (has !== -1 && !socket.destroyed) active.push(socket);
- });
-
- // still leaking sockets
- console.log(active.length, handles.length);
- done();
- });
+ pool.once('end', done);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.