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
This commit is contained in:
Moritz Halbritter
2023-08-10 11:27:04 +02:00
parent d1449fb97c
commit bcee354f54

View File

@@ -29,6 +29,8 @@ import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;
import org.springframework.boot.convert.ApplicationConversionService;
@@ -258,6 +260,8 @@ public class ConfigTreePropertySource extends EnumerablePropertySource<Path> imp
private final Path path;
private final Lock resourceLock = new ReentrantLock();
private final Resource resource;
private final Origin origin;
@@ -341,11 +345,15 @@ public class ConfigTreePropertySource extends EnumerablePropertySource<Path> imp
}
if (this.content == null) {
assertStillExists();
synchronized (this.resource) {
this.resourceLock.lock();
try {
if (this.content == null) {
this.content = FileCopyUtils.copyToByteArray(this.resource.getInputStream());
}
}
finally {
this.resourceLock.unlock();
}
}
return this.content;
}