Skip to content

Commit

Permalink
Ensure non-null reference when replacing
Browse files Browse the repository at this point in the history
If we found a vacated reference above, it could be removed from
the ConcurrentWeakHashMap by the time we get to this point,
resulting in a null return value from computeIfPresent. This new
logic uses compute instead, either replacing the null or vacated
reference with a new one or leaving in place any populated
reference added in parallel somewhere else.

Fixes jruby#8359
  • Loading branch information
headius committed Oct 8, 2024
1 parent 4765ec9 commit 2dec6f6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -4895,7 +4895,9 @@ public RubyString freezeAndDedupString(RubyString string) {
// ref is there but vacated, try to replace it until we have a result
while (true) {
wrapper.string = string;
dedupedRef = dedupMap.computeIfPresent(wrapper, (key, old) -> old.get() == null ? weakref : old);

// re-get reference if it is non-null and populated, or replace with new reference
dedupedRef = dedupMap.compute(wrapper, (key, old) -> old == null || old.get() == null ? weakref : old);

// return result if not vacated
unduped = dedupedRef.get();
Expand Down

0 comments on commit 2dec6f6

Please sign in to comment.