Skip to content

Commit 2a9b094

Browse files
committed
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.
1 parent 44f0f31 commit 2a9b094

File tree

1 file changed

+3
-2
lines changed
  • src/main/java/org/springframework/data/util

1 file changed

+3
-2
lines changed

src/main/java/org/springframework/data/util/Lazy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ public T getNullable() {
133133
return value;
134134
}
135135

136-
this.value = supplier.get();
136+
T result = supplier.get();
137+
this.value = result;
137138
this.resolved = true;
138139

139-
return value;
140+
return result;
140141
}
141142

142143
/**

0 commit comments

Comments
 (0)