Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
🟢 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
approved these changes
Nov 8, 2026
vkarpov15
added a commit
that referenced
this pull request
Nov 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #16611.
Finish
QueryCursorandAggregationCursordestruction when initialization fails or middleware intentionally skips creating a driver cursor. Previously_destroy()could wait forever for a futurecursorevent or callclose()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
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:
0f746df50: all 18 original terminal-initialization regressions fail. Additional connection-aggregation and premature casting-error completion regressions failed against the initial patch before correction.git diff --check: pass.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.