Skip to content

Conversation

@Forostovec
Copy link
Contributor

What was wrong:

Double consumption on early exit: the for key in &mut keys loop consumes the next item before the body runs. If the limiter triggers at loop start, we break, but the current key is already consumed. The subsequent keys.next().is_none() after the loop consumed yet another key.
Misleading done flag: done was derived by advancing the iterator after the loop, not by whether the loop actually exhausted it.

Why this happens

for implicitly calls next() before each iteration.

The extra next() to determine done advanced the iterator once more, skipping work and misreporting completion.

What changed

  1. Loop control:
  • Replaced for key in &mut keys with an explicit loop that checks limiter.is_limit_reached() before calling keys.next().
  1. Compute done within the loop:
  • done = true only when keys.next() returns None.
  • On limit reach, break without advancing and set done = false.

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

this looks correct

I have one style suggestion

Comment on lines 26 to 27
let done;
loop {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we changes this so that we use .peekable on keys.into_iter().peekable()

and then

while keys.peek().is_some() {
     if limiter.is_limit_reached() {
        ...
    key = keys.next()
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

agreed, because I think with these changes we also incorrectly set done = false; if both the limit is reached and there's no more keys left to process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can we changes this so that we use .peekable on keys.into_iter().peekable()

and then

while keys.peek().is_some() {
     if limiter.is_limit_reached() {
        ...
    key = keys.next()
}

Like this? corrected

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah that's also an option @Forostovec

@github-project-automation github-project-automation bot moved this from Backlog to In Progress in Reth Tracker Nov 14, 2025
@mattsse mattsse added the C-bug An unexpected or incorrect behavior label Nov 14, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 14, 2025

CodSpeed Performance Report

Merging #19758 will not alter performance

Comparing Forostovec:fix/prune-iterator-done-overconsumption (b43a25a) with main (cac2443)

Summary

✅ 81 untouched

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

this lgtm, ty!

@mattsse mattsse added this pull request to the merge queue Nov 17, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 17, 2025
@mattsse mattsse added this pull request to the merge queue Nov 17, 2025
@shekhirin shekhirin removed this pull request from the merge queue due to a manual request Nov 17, 2025
@shekhirin shekhirin added this pull request to the merge queue Nov 17, 2025
@shekhirin shekhirin removed this pull request from the merge queue due to a manual request Nov 17, 2025
@mattsse mattsse added this pull request to the merge queue Nov 17, 2025
@mattsse mattsse removed this pull request from the merge queue due to a manual request Nov 17, 2025
@shekhirin shekhirin enabled auto-merge November 17, 2025 15:30
@shekhirin shekhirin added this pull request to the merge queue Nov 17, 2025
Merged via the queue into paradigmxyz:main with commit 90621de Nov 17, 2025
42 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bug An unexpected or incorrect behavior

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants