From 35f4f561535ab7542da07003566b56d5a0895e65 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:04:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20`closed`=20check=20when=20?= =?UTF-8?q?handling=20`querySubscribe()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `Agent` attempts to check if it's been closed when getting the response for a query subscribe. However, it incorrectly tries to access `this` inside a `function`, which doesn't give the correct value, and in `strict mode`, will actually result in an error, since `this` will be `undefined`. This change adds a test for this case and fixes it. --- lib/agent.js | 2 +- test/client/query-subscribe.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/agent.js b/lib/agent.js index 1352d6600..a07650c8a 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -569,7 +569,7 @@ Agent.prototype._querySubscribe = function(queryId, collection, query, options, } this.backend.querySubscribe(this, collection, query, options, function(err, emitter, results, extra) { if (err) return finish(err); - if (this.closed) return emitter.destroy(); + if (agent.closed) return emitter.destroy(); agent._subscribeToQuery(emitter, queryId, collection, query); // No results are returned when ids are passed in as an option. Instead, diff --git a/test/client/query-subscribe.js b/test/client/query-subscribe.js index 95e286f9a..4cf690ff9 100644 --- a/test/client/query-subscribe.js +++ b/test/client/query-subscribe.js @@ -729,6 +729,29 @@ function commonTests(options) { connection.get('dogs', 'fido').on('error', done).create({age: 3}); }); + it('does not reply if the agent is closed before the query returns', function(done) { + var backend = this.backend; + var connection = backend.connect(); + var agent = connection.agent; + + backend.use('query', function(request, next) { + backend.use('reply', function() { + done(new Error('unexpected reply')); + }); + + expect(agent.closed).to.be.false; + agent.stream.on('close', function() { + expect(agent.closed).to.be.true; + next(); + done(); + }); + + agent.close(); + }); + + connection.createSubscribeQuery('dogs', {}); + }); + describe('passing agent.custom to the DB adapter', function() { var connection; var expectedArg = {