Skip to content

Fix stale ConcurrentReferenceHashMap size after compute - #37577

Merged
sbrannen merged 1 commit into
spring-projects:7.0.xfrom
CCCLL:fix/crhm-size-after-compute-removal
Oct 25, 2026
Merged

sbrannen merged 1 commit into
spring-projects:7.0.xfrom
CCCLL:fix/crhm-size-after-compute-removal

Conversation

@CCCLL

@CCCLL CCCLL commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

When compute(), computeIfPresent() or merge() removes an entry
from a ConcurrentReferenceHashMap because the function returns null,
size() and isEmpty() still account for that entry until a later
operation happens to purge released references.

ConcurrentReferenceHashMap<String, String> map = new ConcurrentReferenceHashMap<>();
map.put("a", "1");
map.compute("a", (key, value) -> null);
map.size();     // 1 (0 with this change)
map.isEmpty();  // false (true with this change)

These methods release the reference to the removed entry, but unlike
remove(Object) and remove(Object, Object), their tasks do not use
TaskOption.RESTRUCTURE_AFTER, so the segment count is not updated
right away. This change adds RESTRUCTURE_AFTER to the tasks of
compute(), computeIfPresent() and merge().

This is a regression from the dedicated implementations introduced in
gh-35794: with 6.2.12, the default ConcurrentMap implementations
removed entries through remove(Object, Object), so the map was empty
right away; 6.2.13 and 7.0.x report a size of 1.

The compute(), computeIfPresent() and merge() tests now assert
that the map is empty right after the removal. These assertions fail
without the change and pass with it. ./gradlew :spring-core:check
passes on JDK 25.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 24, 2026
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Oct 25, 2026
@sbrannen sbrannen self-assigned this Oct 25, 2026
@sbrannen sbrannen added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 25, 2026
@sbrannen sbrannen changed the title Fix stale ConcurrentReferenceHashMap size after compute Fix stale ConcurrentReferenceHashMap size after compute Oct 25, 2026
@sbrannen sbrannen added this to the 7.0.11 milestone Oct 25, 2026
Prior to this commit, when compute(), computeIfPresent() or merge()
removed an entry because the function returned null, the reference to
the entry was released, but the segment count was only updated once a
subsequent operation purged released references. As a consequence,
size() and isEmpty() still accounted for the removed entry; for
example, isEmpty() returned false right after removing the only entry.

remove(Object) and remove(Object, Object) restructure the segment after
releasing a reference. This commit applies the same restructuring to
compute(), computeIfPresent() and merge().

See spring-projectsgh-35794
Closes spring-projectsgh-37577

Signed-off-by: Jialin Chen <cccll2019@gmail.com>
@sbrannen
sbrannen force-pushed the fix/crhm-size-after-compute-removal branch from cb0b6cd to 3a88326 Compare October 25, 2026 16:11
@sbrannen
sbrannen changed the base branch from main to 7.0.x October 25, 2026 16:11
@sbrannen
sbrannen merged commit cb0da83 into spring-projects:7.0.x Oct 25, 2026
1 check passed
@github-actions

Copy link
Copy Markdown
Contributor

Fixed via cb0da83

@sbrannen

sbrannen commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Hi @CCCLL,

Congratulations on making your first contribution to the Spring Framework! 👍🏻

This has been merged into 7.0.x and main.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants