Skip to content

Fix PooledHashMap dropping live entries when an entry is removed during forEach#8499

Merged
jack-berg merged 2 commits into
open-telemetry:mainfrom
abdessattar23:fix/pooled-hashmap-issue
Jun 23, 2026
Merged

Fix PooledHashMap dropping live entries when an entry is removed during forEach#8499
jack-berg merged 2 commits into
open-telemetry:mainfrom
abdessattar23:fix/pooled-hashmap-issue

Conversation

@abdessattar23

Copy link
Copy Markdown
Contributor

Noticed PooledHashMap.forEach can skip an entry if the callback removes the one it's currently on. It walks each bucket by index, so remove() shifts the next entry into the current slot and i++ jumps over it

This bites AsynchronousMetricStorage in REUSABLE_DATA mode: during collection it removes stale series while iterating, so a live series sharing a bucket with a stale one can get dropped from that cycle. For cumulative async that shows up as a gap then a jump in the output. (IMMUTABLE_DATA uses ConcurrentHashMap and was fine. The code comment even says the map was picked to allow removal during iteration, it just wasn't holding up)

Fix: iterate each bucket back-to-front so removing the current entry only shifts already-visited ones. Added tests at the map level (collision plus remove-while-iterating) and the storage level (live series survive when stale ones drop)

@abdessattar23 abdessattar23 requested a review from a team as a code owner June 21, 2026 18:12
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 21, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: abdessattar23 / name: Mohammed Abdessetar Elyagoubi (dae39c2)

@abdessattar23 abdessattar23 force-pushed the fix/pooled-hashmap-issue branch from 9a1d1bb to dae39c2 Compare June 21, 2026 18:15
@abdessattar23 abdessattar23 force-pushed the fix/pooled-hashmap-issue branch from dae39c2 to 2d49da6 Compare June 21, 2026 18:21

@jack-berg jack-berg 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.

Nice catch. Build is failing because of formatting. Please run ./gradlew spotlessApply, and commit / push the changes.

// 1 and 17 land in the same bucket, like collection removing a stale series mid-iteration.
PooledHashMap<Integer, Integer> collidingMap = new PooledHashMap<>();
collidingMap.put(1, 1);
collidingMap.put(17, 17);

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.

This test relies on the default capacity changing. Can you either add a comment indicating that if the default capacity changes this test will have to change, or use a different key type that uses a constant hashcode such that keys are guaranteed to be in the same bucket?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks. Used a constant-hashcode key so the collision doesn't depend on capacity anymore

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.50%. Comparing base (824334c) to head (d94ae0c).
⚠️ Report is 14 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8499      +/-   ##
============================================
- Coverage     78.77%   78.50%   -0.27%     
- Complexity     8579     8601      +22     
============================================
  Files          1009     1013       +4     
  Lines         28993    29150     +157     
  Branches       3599     3632      +33     
============================================
+ Hits          22839    22885      +46     
- Misses         5311     5421     +110     
- Partials        843      844       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abdessattar23

Copy link
Copy Markdown
Contributor Author

@jack-berg Now all jobs are succeeding and tests too, I believe it's ready now :)

@jack-berg jack-berg 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.

Thanks!

@jack-berg jack-berg merged commit 497b26d into open-telemetry:main Jun 23, 2026
28 checks passed
@otelbot

otelbot Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution @abdessattar23! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

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.

2 participants