Skip to content

Fix CachingIterator::count() ending the current iteration - #2034

Merged
GromNaN merged 1 commit into
mongodb:v2.5from
zigzagdev:fix-caching-iterator-count
Oct 22, 2026
Merged

GromNaN merged 1 commit into
mongodb:v2.5from
zigzagdev:fix-caching-iterator-count

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

CachingIterator, returned by Collection::listIndexes(), Collection::listSearchIndexes() and Database::listCollections(), exhausts its wrapped iterator in count() to cache all results. That exhaustion called next(), which also moved the position in the cache to the end, so calling count() during a foreach silently ended the loop. The wrapped iterator is now exhausted without moving the current position.

@zigzagdev
zigzagdev requested a review from a team as a code owner October 19, 2026 11:39
@zigzagdev
zigzagdev requested a review from paulinevos October 19, 2026 11:39
@zigzagdev zigzagdev changed the title Do not move the position of CachingIterator when counting it fix: Don't move the position of CachingIterator when counting it Oct 19, 2026
@zigzagdev zigzagdev changed the title fix: Don't move the position of CachingIterator when counting it fix: Don't move the position of CachingIterator when counting itself Oct 19, 2026
@GromNaN GromNaN added the bug a bug fix label Oct 19, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@GromNaN
GromNaN force-pushed the fix-caching-iterator-count branch from 68f61da to b9884d5 Compare October 22, 2026 20:27
@GromNaN
GromNaN requested review from GromNaN and removed request for paulinevos October 22, 2026 20:38
count() exhausted the inner iterator by calling next(), which also advanced
the position in the cache. Calling count() while iterating over the result of
Collection::listIndexes() or Database::listCollections() therefore ended the
iteration early. The inner iterator is now exhausted without moving the
position in the cache.
@GromNaN
GromNaN force-pushed the fix-caching-iterator-count branch from b9884d5 to 93f4e36 Compare October 22, 2026 21:05
@GromNaN
GromNaN changed the base branch from v2.x to v2.5 October 22, 2026 21:05
@GromNaN GromNaN changed the title fix: Don't move the position of CachingIterator when counting itself Fix CachingIterator::count() ending the current iteration Oct 22, 2026

@GromNaN GromNaN left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you @zigzagdev

@GromNaN
GromNaN merged commit 56088c0 into mongodb:v2.5 Oct 22, 2026
39 checks passed
This was referenced Oct 22, 2026
@zigzagdev
zigzagdev deleted the fix-caching-iterator-count branch November 6, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug a bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants