Skip to content

Commit

Permalink
🐛 Fix closed check when handling querySubscribe()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alecgibson committed Oct 17, 2024
1 parent 4b8d3dc commit 35f4f56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions test/client/query-subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 35f4f56

Please sign in to comment.