GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Don't return an entry twice when removing while iterating a primitive map by CCCLL · Pull Request #18070 · netty/netty

Conversation

@CCCLL

@CCCLL CCCLL commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Motivation:

The iterators of IntObjectHashMap and the other generated primitive maps scan the arrays from index 0. remove() calls removeAt(...), which moves the displaced entries that follow the removed one back into the free slot. That run of entries can wrap around from the end of the arrays to index 0, so an entry at the start of the arrays, which the iterator has already returned, can be moved to a slot that is still ahead of the iterator and is returned again:

IntObjectHashMap<String> map = new IntObjectHashMap<>(8);
map.put(6, "a");   // index 6
map.put(14, "b");  // hashes to 6, stored at index 7
map.put(15, "c");  // hashes to 7, wraps around to index 0
Iterator<PrimitiveEntry<String>> it = map.entries().iterator();
while (it.hasNext()) {
    int key = it.next().key();
    System.out.println(key);
    if (key != 15) {
        it.remove();
    }
}
// prints 15, 6, 14, 15

The same happens through entrySet(), keySet() and values(). Entries are only ever returned twice, never skipped. This has been the case since remove() started moving entries back instead of leaving tombstones (07024a4, 2014). The callers in Netty that remove while iterating (DefaultHttp2Connection.close(...) and WeightedFairQueueByteDistributor) remove the entries they visit, so I did not find an affected caller in Netty itself. Code that removes only some of the entries can see an entry twice.

Modification:

  • Start the iteration right after an empty slot and wrap around at the end of the arrays. A run of occupied slots ends before that empty slot, so removeAt(...) never moves an entry that was already returned to a slot ahead of the iterator. calcMaxSize(int) ensures that there is always an empty slot.
  • Add iteratorRemoveShouldNotReturnEntriesTwice to the map tests.

Result:

Removing entries while iterating returns every entry exactly once. The example above prints 6, 14, 15.

The new test fails for all five map types before the change and passes after it. I also ran a randomized check with LongObjectHashMap (capacity 1 to 64, load factor 0.1 to 0.99, up to 48 keys, removing each visited entry with a random probability through entries(), entrySet() and values()) that every key is returned exactly once and that the map ends up with the expected content: 1,899 of 300,000 runs failed with 4.2.19.Final, none of 600,000 runs failed with this change. Tests of common, codec-http, codec-http2, codec-mqtt, codec-redis, codec-dns and resolver-dns pass. I could not run the codec-http3 tests locally because they need the native QUIC library; its settings tests check the content, not the order.

The iteration order changes when index 0 is occupied. The order of these maps is not specified, and the tests above do not depend on it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@CCCLL
Don't return an entry twice when removing while iterating a primitive…
226ad1a
… map

Motivation:

The iterators of IntObjectHashMap and the other generated primitive maps
scan the arrays from index 0. remove() calls removeAt(...), which moves
the displaced entries that follow the removed one back into the free
slot. That run of entries can wrap around from the end of the arrays to
index 0, so an entry at the start of the arrays, which the iterator has
already returned, can be moved to a slot that is still ahead of the
iterator and is returned again. For example, with a capacity of 8 the
keys 6, 14 and 15 are stored at index 6, 7 and 0, and removing 6 and 14
while iterating returns 15 twice.

Modification:

- Start the iteration right after an empty slot and wrap around at the
  end of the arrays. A run of occupied slots ends before that empty
  slot, so removeAt(...) never moves an entry that was already returned
  to a slot ahead of the iterator. calcMaxSize(int) ensures that there
  is always an empty slot.
- Add iteratorRemoveShouldNotReturnEntriesTwice to the map tests.

Result:

Removing entries while iterating returns every entry exactly once.
@testlens-app

testlens-app Bot commented Oct 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 226ad1a
▶️ Tests: 54657 executed
🟡 Checks: 30/33 completed


Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest approved these changes Oct 24, 2026
View reviewed changes
@chrisvest chrisvest added this to the 4.2.20.Final milestone Oct 24, 2026
@chrisvest chrisvest added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Oct 24, 2026
@chrisvest
chrisvest merged commit 0ba649e into netty:4.2 Oct 24, 2026
29 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot

netty-project-bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #18085

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Oct 24, 2026
Auto-port 5.0: Don't return an entry twice when removing while iterating a primitive map #18085
Merged
@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 24, 2026
@netty-project-bot netty-project-bot mentioned this pull request Oct 24, 2026
Auto-port 4.1: Don't return an entry twice when removing while iterating a primitive map #18087
Merged
@netty-project-bot

netty-project-bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #18087

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Oct 24, 2026
normanmaurer pushed a commit that referenced this pull request Oct 24, 2026
@netty-project-bot @CCCLL
Auto-port 5.0: Don't return an entry twice when removing while iterat…
9b19efb
…ing a primitive map (#18085)

Auto-port of #18070 to 5.0
Cherry-picked commit: 0ba649e

---
Motivation:

The iterators of `IntObjectHashMap` and the other generated primitive
maps scan the arrays from index 0. `remove()` calls `removeAt(...)`,
which moves the displaced entries that follow the removed one back into
the free slot. That run of entries can wrap around from the end of the
arrays to index 0, so an entry at the start of the arrays, which the
iterator has already returned, can be moved to a slot that is still
ahead of the iterator and is returned again:

```java
IntObjectHashMap<String> map = new IntObjectHashMap<>(8);
map.put(6, "a");   // index 6
map.put(14, "b");  // hashes to 6, stored at index 7
map.put(15, "c");  // hashes to 7, wraps around to index 0
Iterator<PrimitiveEntry<String>> it = map.entries().iterator();
while (it.hasNext()) {
    int key = it.next().key();
    System.out.println(key);
    if (key != 15) {
        it.remove();
    }
}
// prints 15, 6, 14, 15
```

The same happens through `entrySet()`, `keySet()` and `values()`.
Entries are only ever returned twice, never skipped. This has been the
case since `remove()` started moving entries back instead of leaving
tombstones (07024a4, 2014). The callers in Netty that remove while
iterating (`DefaultHttp2Connection.close(...)` and
`WeightedFairQueueByteDistributor`) remove the entries they visit, so I
did not find an affected caller in Netty itself. Code that removes only
some of the entries can see an entry twice.

Modification:

- Start the iteration right after an empty slot and wrap around at the
end of the arrays. A run of occupied slots ends before that empty slot,
so `removeAt(...)` never moves an entry that was already returned to a
slot ahead of the iterator. `calcMaxSize(int)` ensures that there is
always an empty slot.
- Add `iteratorRemoveShouldNotReturnEntriesTwice` to the map tests.

Result:

Removing entries while iterating returns every entry exactly once. The
example above prints `6, 14, 15`.

The new test fails for all five map types before the change and passes
after it. I also ran a randomized check with `LongObjectHashMap`
(capacity 1 to 64, load factor 0.1 to 0.99, up to 48 keys, removing each
visited entry with a random probability through `entries()`,
`entrySet()` and `values()`) that every key is returned exactly once and
that the map ends up with the expected content: 1,899 of 300,000 runs
failed with 4.2.19.Final, none of 600,000 runs failed with this change.
Tests of `common`, `codec-http`, `codec-http2`, `codec-mqtt`,
`codec-redis`, `codec-dns` and `resolver-dns` pass. I could not run the
`codec-http3` tests locally because they need the native QUIC library;
its settings tests check the content, not the order.

The iteration order changes when index 0 is occupied. The order of these
maps is not specified, and the tests above do not depend on it.

Co-authored-by: CCCLL <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 24, 2026
@netty-project-bot @CCCLL
Auto-port 4.1: Don't return an entry twice when removing while iterat…
0a82f74
…ing a primitive map (#18087)

Auto-port of #18070 to 4.1
Cherry-picked commit: 0ba649e

---
Motivation:

The iterators of `IntObjectHashMap` and the other generated primitive
maps scan the arrays from index 0. `remove()` calls `removeAt(...)`,
which moves the displaced entries that follow the removed one back into
the free slot. That run of entries can wrap around from the end of the
arrays to index 0, so an entry at the start of the arrays, which the
iterator has already returned, can be moved to a slot that is still
ahead of the iterator and is returned again:

```java
IntObjectHashMap<String> map = new IntObjectHashMap<>(8);
map.put(6, "a");   // index 6
map.put(14, "b");  // hashes to 6, stored at index 7
map.put(15, "c");  // hashes to 7, wraps around to index 0
Iterator<PrimitiveEntry<String>> it = map.entries().iterator();
while (it.hasNext()) {
    int key = it.next().key();
    System.out.println(key);
    if (key != 15) {
        it.remove();
    }
}
// prints 15, 6, 14, 15
```

The same happens through `entrySet()`, `keySet()` and `values()`.
Entries are only ever returned twice, never skipped. This has been the
case since `remove()` started moving entries back instead of leaving
tombstones (07024a4, 2014). The callers in Netty that remove while
iterating (`DefaultHttp2Connection.close(...)` and
`WeightedFairQueueByteDistributor`) remove the entries they visit, so I
did not find an affected caller in Netty itself. Code that removes only
some of the entries can see an entry twice.

Modification:

- Start the iteration right after an empty slot and wrap around at the
end of the arrays. A run of occupied slots ends before that empty slot,
so `removeAt(...)` never moves an entry that was already returned to a
slot ahead of the iterator. `calcMaxSize(int)` ensures that there is
always an empty slot.
- Add `iteratorRemoveShouldNotReturnEntriesTwice` to the map tests.

Result:

Removing entries while iterating returns every entry exactly once. The
example above prints `6, 14, 15`.

The new test fails for all five map types before the change and passes
after it. I also ran a randomized check with `LongObjectHashMap`
(capacity 1 to 64, load factor 0.1 to 0.99, up to 48 keys, removing each
visited entry with a random probability through `entries()`,
`entrySet()` and `values()`) that every key is returned exactly once and
that the map ends up with the expected content: 1,899 of 300,000 runs
failed with 4.2.19.Final, none of 600,000 runs failed with this change.
Tests of `common`, `codec-http`, `codec-http2`, `codec-mqtt`,
`codec-redis`, `codec-dns` and `resolver-dns` pass. I could not run the
`codec-http3` tests locally because they need the native QUIC library;
its settings tests check the content, not the order.

The iteration order changes when index 0 is occupied. The order of these
maps is not specified, and the tests above do not depend on it.

Co-authored-by: CCCLL <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chrisvest chrisvest chrisvest approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.20.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@CCCLL @netty-project-bot @chrisvest
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.