From 1ad59384ec55427dcac821fc04e4c003e522c7ec Mon Sep 17 00:00:00 2001 From: Curran Kelleher Date: Tue, 9 Apr 2019 08:23:56 +0530 Subject: [PATCH 1/3] Clean up closed connections. Closes #282 --- lib/agent.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index ae2f1a82a..e3095b2b8 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -258,12 +258,15 @@ Agent.prototype._open = function() { agent._handleMessage(request.data, callback); }); }); - - this.stream.on('end', function() { + + function cleanup() { agent.backend.agentsCount--; if (!agent.stream.isServer) agent.backend.remoteAgentsCount--; agent._cleanup(); - }); + } + + this.stream.on('end', cleanup); + this.stream.on('close', cleanup); }; // Check a request to see if its valid. Returns an error if there's a problem. From 186e2abffc17713fae98883060f02aa07b381f37 Mon Sep 17 00:00:00 2001 From: curran Date: Tue, 9 Apr 2019 15:53:10 +0530 Subject: [PATCH 2/3] Add test coverage for emitting 'close'. --- test/client/connection.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/client/connection.js b/test/client/connection.js index e6a9aedfd..1f72c586c 100644 --- a/test/client/connection.js +++ b/test/client/connection.js @@ -101,6 +101,18 @@ describe('client connection', function() { }); }); + it('updates after connection socket stream emits "close"', function(done) { + var backend = this.backend; + var connection = backend.connect(); + connection.on('connected', function() { + connection.socket.stream.emit('close') + setTimeout(function() { + expect(backend.agentsCount).equal(0); + done(); + }, 10); + }); + }); + it('does not increment when agent connect is rejected', function() { var backend = this.backend; backend.use('connect', function(request, next) { From ef236fae5ff91c1b2c7790dab5dea9f804558352 Mon Sep 17 00:00:00 2001 From: curran Date: Tue, 9 Apr 2019 16:02:59 +0530 Subject: [PATCH 3/3] Handle case of emitting both 'end' and 'close'. Closes #282 --- lib/agent.js | 14 ++++++++------ test/client/connection.js | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index e3095b2b8..b5cef65c1 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -56,8 +56,15 @@ Agent.prototype.close = function(err) { }; Agent.prototype._cleanup = function() { + + // Only clean up once if the stream emits both 'end' and 'close'. + if (this.closed) return; + this.closed = true; + this.backend.agentsCount--; + if (!this.stream.isServer) this.backend.remoteAgentsCount--; + // Clean up doc subscription streams for (var collection in this.subscribedDocs) { var docs = this.subscribedDocs[collection]; @@ -259,12 +266,7 @@ Agent.prototype._open = function() { }); }); - function cleanup() { - agent.backend.agentsCount--; - if (!agent.stream.isServer) agent.backend.remoteAgentsCount--; - agent._cleanup(); - } - + var cleanup = agent._cleanup.bind(agent); this.stream.on('end', cleanup); this.stream.on('close', cleanup); }; diff --git a/test/client/connection.js b/test/client/connection.js index 1f72c586c..aa00e2db8 100644 --- a/test/client/connection.js +++ b/test/client/connection.js @@ -106,10 +106,19 @@ describe('client connection', function() { var connection = backend.connect(); connection.on('connected', function() { connection.socket.stream.emit('close') - setTimeout(function() { - expect(backend.agentsCount).equal(0); - done(); - }, 10); + expect(backend.agentsCount).equal(0); + done(); + }); + }); + + it('updates correctly after stream emits both "end" and "close"', function(done) { + var backend = this.backend; + var connection = backend.connect(); + connection.on('connected', function() { + connection.socket.stream.emit('end') + connection.socket.stream.emit('close') + expect(backend.agentsCount).equal(0); + done(); }); });