From ffe1946f1317bf780f0970960c0d67874edf3b92 Mon Sep 17 00:00:00 2001 From: jbj338033 Date: Mon, 22 Sep 2025 20:14:04 +0900 Subject: [PATCH] Fix potential race condition in Lazy.getNullable() The getNullable() method had a subtle race condition where the value field could be read as null after being assigned but before the resolved flag was set to true. This could occur if another thread read the value between lines 136 and 137. By using a local variable to store the supplier's result and returning that directly, we ensure that the thread that computes the value will always return the correct result, regardless of what other threads observe. This change maintains the existing behavior documented in the class Javadoc that the supplier may be called multiple times under concurrent access, while fixing the potential for incorrect null returns. Signed-off-by: jbj338033 --- src/main/java/org/springframework/data/util/Lazy.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/util/Lazy.java b/src/main/java/org/springframework/data/util/Lazy.java index 643ecbc95c..193726da60 100644 --- a/src/main/java/org/springframework/data/util/Lazy.java +++ b/src/main/java/org/springframework/data/util/Lazy.java @@ -133,10 +133,11 @@ public T getNullable() { return value; } - this.value = supplier.get(); + T result = supplier.get(); + this.value = result; this.resolved = true; - return value; + return result; } /**