Skip to content

Commit 523e95e

Browse files
Since we are locking above, it makes sense to lock in the exception. And we better use a ReentrantLock I think. At least it will prevent virtual thread pinning
Signed-off-by: Henri Tremblay <[email protected]>
1 parent 50970d6 commit 523e95e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashSet;
2525
import java.util.List;
2626
import java.util.Set;
27+
import java.util.concurrent.locks.ReentrantLock;
2728

2829
import io.micrometer.observation.ObservationRegistry;
2930
import org.eclipse.jgit.api.CheckoutCommand;
@@ -155,7 +156,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
155156
* both the ResourceController and the EnvironmentController. See <a href=
156157
* "https://github.com/spring-cloud/spring-cloud-config/issues/2681">#2681</a>.
157158
*/
158-
private final Object LOCK = new Object();
159+
private final ReentrantLock LOCK = new ReentrantLock();
159160

160161
public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnvironmentProperties properties,
161162
ObservationRegistry observationRegistry) {
@@ -262,9 +263,13 @@ public void setSkipSslValidation(boolean skipSslValidation) {
262263

263264
@Override
264265
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
265-
synchronized (LOCK) {
266+
LOCK.lock();
267+
try {
266268
return super.findOne(application, profile, label, includeOrigin);
267269
}
270+
finally {
271+
LOCK.unlock();
272+
}
268273
}
269274

270275
@Override
@@ -274,16 +279,26 @@ public synchronized Locations getLocations(String application, String profile, S
274279
}
275280
String version;
276281
try {
277-
synchronized (LOCK) {
282+
try {
283+
LOCK.lock();
278284
version = refresh(label);
279285
}
286+
finally {
287+
LOCK.unlock();
288+
}
280289
}
281290
catch (Exception e) {
282291
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
283292
&& tryMasterBranch) {
284293
logger.info("Could not refresh default label " + label, e);
285294
logger.info("Will try to refresh master label instead.");
286-
version = refresh(JGitEnvironmentProperties.MASTER_LABEL);
295+
LOCK.lock();
296+
try {
297+
version = refresh(JGitEnvironmentProperties.MASTER_LABEL);
298+
}
299+
finally {
300+
LOCK.unlock();
301+
}
287302
}
288303
else {
289304
throw e;

0 commit comments

Comments
 (0)