From bedf5c35372aac7b29a093047761ffadd9d42369 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 16 Nov 2015 14:46:32 -0600 Subject: [PATCH] [transport] prevent the transport from leaking timer id's --- src/lib/transport.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/transport.js b/src/lib/transport.js index 3e3575336..d20a02703 100644 --- a/src/lib/transport.js +++ b/src/lib/transport.js @@ -319,8 +319,10 @@ Transport.prototype.request = function (params, cb) { }; Transport.prototype._timeout = function (cb, delay) { - this._timers = this._timers || []; + if (this.closed) return; + var id; + var timers = this._timers || (this._timers = []); if ('function' !== typeof cb) { id = cb; @@ -329,8 +331,12 @@ Transport.prototype._timeout = function (cb, delay) { if (cb) { // set the timer - id = setTimeout(cb, delay); - this._timers.push(id); + id = setTimeout(function () { + _.pull(timers, id); + cb(); + }, delay); + + timers.push(id); return id; } @@ -393,6 +399,8 @@ Transport.prototype.sniff = function (cb) { */ Transport.prototype.close = function () { this.log.close(); + this.closed = true; _.each(this._timers, clearTimeout); + this._timers = null; this.connectionPool.close(); };