From 3412d4c0dd5250f14cc6fe594355bdf2ff976417 Mon Sep 17 00:00:00 2001 From: Per Minborg Date: Sat, 25 Jul 2026 07:54:18 +0000 Subject: [PATCH] 8387722: The race during initialization or lazy constants in and Map/Set Reviewed-by: jvernee --- src/java.base/share/classes/java/util/LazyCollections.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/java.base/share/classes/java/util/LazyCollections.java b/src/java.base/share/classes/java/util/LazyCollections.java index e15721db2ce..f74fbad358b 100644 --- a/src/java.base/share/classes/java/util/LazyCollections.java +++ b/src/java.base/share/classes/java/util/LazyCollections.java @@ -675,6 +675,12 @@ final class LazyCollections { final long offset = offsetFor(index); final Object mutex = mutexes.acquireMutex(offset); if (mutex == null) { + // There might be a race where the value is already computed and + // the mutex is cleared, so we need to re-check the value again + final T t = (T) UNSAFE.getReferenceAcquire(array, offset); + if (t != null) { + return t; + } throwIfPreviousException(index, throwables, input); // There must be an exception throw cannotReachHere(functionHolder, input);