Skip to content

fix(cursor): finish destruction after terminal initialization - #16612

Merged
vkarpov15 merged 4 commits into
Automattic:masterfrom
IbrahimHafez1:codex/cursor-destroy-initialization
Nov 8, 2026
Merged

vkarpov15 merged 4 commits into
Automattic:masterfrom
IbrahimHafez1:codex/cursor-destroy-initialization

Conversation

@IbrahimHafez1

@IbrahimHafez1 IbrahimHafez1 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #16611.

Finish QueryCursor and AggregationCursor destruction when initialization fails or middleware intentionally skips creating a driver cursor. Previously _destroy() could wait forever for a future cursor event or call close() on null.

Track initialization completion separately from _error, including connection-level aggregation. While pending, a shared waiter observes readiness/error events but resolves only after an actual terminal initialization result. A casting error can be emitted while an asynchronous pre-find hook is still pending; treating that event as completion would miss closing the subsequently created cursor. The waiter removes only its own listeners once initialization completes.

Close an existing cursor, await its cleanup, then complete destruction. Existing cleanup-error propagation and successful cleanup timing remain intact. There is one boolean per wrapper, no polling, no new database operations and no per-document work. The new waiter is allocated only when destruction must wait for initialization.

Examples

schema.pre('find', async function() { throw new Error('pre hook failed'); });
const cursor = Model.find().cursor();
await once(cursor, 'error');
cursor.destroy();
// Before: destroyed === true, closed === false indefinitely.
// After: destruction completes and close fires.

Twenty-five regression/control cases cover destruction before/after hook failure and skips, pipeline failures, connection-level aggregation, casting errors stored/emitted before readiness, synchronous driver-cursor creation failures, error-listener destruction, and temporary listener cleanup.

Validation on Windows, Node 22.17.1, MongoDB 8.2.3:

Related #16610 preserves an explicit destruction error and avoids an additional notification when Mongoose already emitted that same error. Combined tests cover their interaction. Neither PR redesigns all pre-existing manual error emission paths.

Replica-set, sharding and encryption configurations were not enabled locally. Current hosted results are shown by GitHub; this is not a claim of a completely green local full run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the changes to cursor destruction after initialization failures or skipped cursor creation.

No open issues 🎉

What changed in this PR

Fixes #16611 by allowing query and aggregation cursor destruction to complete after initialization fails or middleware skips creating a driver cursor, while preserving cleanup ordering.

Changes:

  • Tracks terminal initialization separately from stored errors.
  • Adds a shared waiter that ignores premature errors and removes its own listeners once initialization completes.
  • Adds 25 regression and control cases covering failures, skips, pending initialization, and listener cleanup.
File Description
test/​cursor.destroy.initialization.test.js Tests destruction across terminal and pending initialization paths.
lib/​helpers/​cursor/​waitForInitialization.js Adds shared initialization waiting with targeted listener cleanup.
lib/​cursor/​queryCursor.js Tracks initialization completion and safely destroys queries without driver cursors.
lib/​cursor/​aggregationCursor.js Applies initialization-aware destruction to model and connection aggregation cursors.

Review effort: Balanced

@vkarpov15 vkarpov15 added this to the 9.12.1 milestone Nov 8, 2026
@vkarpov15
vkarpov15 merged commit 48267f0 into Automattic:master Nov 8, 2026
32 checks passed
vkarpov15 added a commit that referenced this pull request Nov 8, 2026
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.

Cursor destruction never completes after failed or skipped initialization

3 participants