diff --git a/lib/net/bip151.js b/lib/net/bip151.js index f69fbade..10695c3f 100644 --- a/lib/net/bip151.js +++ b/lib/net/bip151.js @@ -323,6 +323,7 @@ function BIP151(cipher) { this.timeout = null; this.job = null; + this.onShake = null; this.bip150 = null; } @@ -472,8 +473,15 @@ BIP151.prototype.cleanup = function cleanup() { this.completed = true; this.job = null; - clearTimeout(this.timeout); - this.timeout = null; + if (this.timeout != null) { + clearTimeout(this.timeout); + this.timeout = null; + } + + if (this.onShake) { + this.removeListener('handshake', this.onShake); + this.onShake = null; + } return job; }; @@ -530,9 +538,8 @@ BIP151.prototype._wait = function wait(timeout, resolve, reject) { self.reject(new Error('BIP151 handshake timed out.')); }, timeout); - this.once('handshake', function() { - self.resolve(); - }); + this.onShake = this.resolve.bind(this); + this.once('handshake', this.onShake); }; /** @@ -544,6 +551,11 @@ BIP151.prototype.destroy = function destroy() { clearTimeout(this.timeout); this.timeout = null; } + + if (this.onShake) { + this.removeListener('handshake', this.onShake); + this.onShake = null; + } }; /**