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 = {