Skip to content

Commit bcee354

Browse files
committed
Refactor synchronized to Lock in ConfigTreePropertySource
The synchronized guards an I/O operation. This code path can be triggered every time a user calls .getProperty() on the Environment. See gh-36670
1 parent d1449fb commit bcee354

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/ConfigTreePropertySource.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Map;
3030
import java.util.Set;
3131
import java.util.TreeMap;
32+
import java.util.concurrent.locks.Lock;
33+
import java.util.concurrent.locks.ReentrantLock;
3234
import java.util.stream.Stream;
3335

3436
import org.springframework.boot.convert.ApplicationConversionService;
@@ -258,6 +260,8 @@ private static final class PropertyFileContent implements Value, OriginProvider
258260

259261
private final Path path;
260262

263+
private final Lock resourceLock = new ReentrantLock();
264+
261265
private final Resource resource;
262266

263267
private final Origin origin;
@@ -341,11 +345,15 @@ private byte[] getBytes() {
341345
}
342346
if (this.content == null) {
343347
assertStillExists();
344-
synchronized (this.resource) {
348+
this.resourceLock.lock();
349+
try {
345350
if (this.content == null) {
346351
this.content = FileCopyUtils.copyToByteArray(this.resource.getInputStream());
347352
}
348353
}
354+
finally {
355+
this.resourceLock.unlock();
356+
}
349357
}
350358
return this.content;
351359
}

0 commit comments

Comments
 (0)