Repository navigation
Conversation
computeIfAbsent registered the case-insensitive key before invoking the mapping function. If the function returned null or threw an exception, no mapping was recorded in the target map, but the key registration was left behind. The map then reported containsKey(key) as true while size() was 0, keySet() was empty, and get(key) returned null. A later insertion with a different casing also reused the stale casing of the failed call, since the existing-key branch resolved to the registered key. The case-insensitive key is now only looked up up front. For a new key, it is registered from within the mapping function once a non-null value has been computed, which is still before the entry is inserted. A null result or an exception therefore leaves both maps untouched, while a removeEldestEntry override that evicts the new entry right away still removes the registration, as it does for put. The existing-key path keeps computing under the stored casing. Closes spring-projectsgh-37351 Signed-off-by: junhyeong9812 <pickjog@gmail.com>
sbrannen
force-pushed
the
fix/lcim-computeifabsent-phantom-key
branch
from
September 30, 2026 10:45
95a1db9 to
b48e801
Compare
Contributor
|
Fixed via 1b4955e |
Member
|
This has been merged into Thanks |
Contributor
Author
|
Thanks for reviewing and merging this, and for the backport to 7.0.x. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
LinkedCaseInsensitiveMap.computeIfAbsentregisters the case-insensitive key before the mapping function runs. When the function returnsnullor throws,Map#computeIfAbsentrecords no mapping, but the registration stays behind. The map then reports a key it does not contain:containsKey(key)istruewhilesize()is0,keySet()is empty andget(key)returnsnull. This PR registers the key only once a value has actually been computed.Problem
LinkedCaseInsensitiveMapkeeps two structures in sync:targetMap, which holds the entries under their original casing, andcaseInsensitiveKeys, which maps each converted key to the stored key.containsKeyconsults the latter, whilesize,keySetandgetgo through the former.computeIfAbsentcalledcaseInsensitiveKeys.putIfAbsent(convertKey(key), key)up front and then delegated totargetMap.computeIfAbsent(key, mappingFunction). Per theMap#computeIfAbsentcontract, anullresult or an exception from the mapping function records no mapping, so the two structures disagree afterwards:The stale registration also leaks into later insertions:
map.computeIfAbsent("KEY", key -> "value")then finds the registered key"Key", takes the existing-key branch (introduced for gh-26868) and stores the value under"Key"rather than the caller's"KEY".The in-tree callers (
StompHeaders,NativeMessageHeaderAccessor,MockHttpServletResponse) always return a non-null value, so this surfaces with user code that usescomputeIfAbsentas a lookup cache, where anullresult or an exception is an expected outcome.Fix
computeIfAbsentnow only looks up the case-insensitive key up front. The existing-key path is unchanged in behavior: an existing non-null value is returned as is, and an existingnullvalue is computed under the stored casing. For a new key, the mapping function is wrapped so that the case-insensitive key is registered right after a non-null value has been computed, beforetargetMapinserts the entry. Anullresult or an exception therefore leaves both structures untouched.Registering before the insertion (rather than after
targetMap.computeIfAbsentreturns) matters for subclasses overridingremoveEldestEntry:LinkedHashMapinvokes it during the insertion, and the internal hook removes the evicted key's registration. Registering afterwards would re-add the registration for an entry that was evicted within the same call; with the registration in place beforehand, eviction cleans it up the same way it does forput.New tests cover a
nullresult, a throwing mapping function, the casing of a subsequent insertion, an existingnull-valued entry with anullresult, and an entry evicted immediately byremoveEldestEntry. The full spring-core test suite passes.