Skip to content

Commit 35f4f56

Browse files
committed
🐛 Fix closed check when handling querySubscribe()
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.
1 parent 4b8d3dc commit 35f4f56

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ Agent.prototype._querySubscribe = function(queryId, collection, query, options,
569569
}
570570
this.backend.querySubscribe(this, collection, query, options, function(err, emitter, results, extra) {
571571
if (err) return finish(err);
572-
if (this.closed) return emitter.destroy();
572+
if (agent.closed) return emitter.destroy();
573573

574574
agent._subscribeToQuery(emitter, queryId, collection, query);
575575
// No results are returned when ids are passed in as an option. Instead,

test/client/query-subscribe.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,29 @@ function commonTests(options) {
729729
connection.get('dogs', 'fido').on('error', done).create({age: 3});
730730
});
731731

732+
it('does not reply if the agent is closed before the query returns', function(done) {
733+
var backend = this.backend;
734+
var connection = backend.connect();
735+
var agent = connection.agent;
736+
737+
backend.use('query', function(request, next) {
738+
backend.use('reply', function() {
739+
done(new Error('unexpected reply'));
740+
});
741+
742+
expect(agent.closed).to.be.false;
743+
agent.stream.on('close', function() {
744+
expect(agent.closed).to.be.true;
745+
next();
746+
done();
747+
});
748+
749+
agent.close();
750+
});
751+
752+
connection.createSubscribeQuery('dogs', {});
753+
});
754+
732755
describe('passing agent.custom to the DB adapter', function() {
733756
var connection;
734757
var expectedArg = {

0 commit comments

Comments
 (0)