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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user