Skip to content

Fix wrong this context in QueryCursor._read cursor close callback#16104

Merged
vkarpov15 merged 1 commit intoAutomattic:masterfrom
techcodie:fix-query-cursor-close-context
Mar 17, 2026
Merged

Fix wrong this context in QueryCursor._read cursor close callback#16104
vkarpov15 merged 1 commit intoAutomattic:masterfrom
techcodie:fix-query-cursor-close-context

Conversation

@techcodie
Copy link
Copy Markdown
Contributor

Fix wrong this context in QueryCursor._read cursor close callback

Problem:
In QueryCursor.prototype._read, the cursor.close() callback used a regular function, causing this to be undefined. This meant any error during cursor close would fail silently instead of being emitted on the QueryCursor instance.

Fix:
Changed the regular function to an arrow function so this correctly refers to the QueryCursor instance - consistent with how AggregationCursor._read already handles it.

// Before
this.cursor.close(function(error) {
  if (error) {
    return this.emit('error', error); // `this` is wrong here
  }
});

// After
this.cursor.close((error) => {
  if (error) {
    return this.emit('error', error); // `this` correctly refers to QueryCursor
  }
});

Note: AggregationCursor._read already handles this correctly using _this. This PR makes QueryCursor consistent with that pattern.

Using a regular function in cursor.close() callback caused  to be
undefined, making error emission fail silently. Changed to arrow function
to correctly reference the QueryCursor instance.
@techcodie
Copy link
Copy Markdown
Contributor Author

The failing test (sessions gh-6362 - sets session when pulling a document from db) is a pre-existing flaky timing test unrelated to this change. It checks session.serverSession.lastUse timestamps which can fail intermittently based on server response timing. Our change is limited to a one-line fix in queryCursor.js.

Copy link
Copy Markdown
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 requested a review from Copilot March 17, 2026 15:16
@vkarpov15 vkarpov15 added this to the 9.3.1 milestone Mar 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates QueryCursor stream end-of-cursor handling to use an arrow function for the cursor.close() callback, ensuring the callback retains the QueryCursor instance context when emitting errors.

Changes:

  • Replace function(error) with (error) => { ... } in the cursor.close() callback within _read()

@vkarpov15 vkarpov15 merged commit 89ae6ec into Automattic:master Mar 17, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants