Add a lock to prevent race condition between EnvironmentController and ResourceController (#2737)
Fixes #2681
This commit is contained in:
@@ -56,6 +56,7 @@ import org.eclipse.jgit.transport.TrackingRefUpdate;
|
||||
import org.eclipse.jgit.util.FileUtils;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.cloud.config.environment.Environment;
|
||||
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
@@ -149,6 +150,13 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
|
||||
|
||||
private final ObservationRegistry observationRegistry;
|
||||
|
||||
/**
|
||||
* This lock is used to ensure thread safety between accessing the local git repo from
|
||||
* both the ResourceController and the EnvironmentController. See
|
||||
* <a href="https://github.com/spring-cloud/spring-cloud-config/issues/2681">#2681</a>.
|
||||
*/
|
||||
private final Object LOCK = new Object();
|
||||
|
||||
public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnvironmentProperties properties,
|
||||
ObservationRegistry observationRegistry) {
|
||||
super(environment, properties, observationRegistry);
|
||||
@@ -252,6 +260,13 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
|
||||
this.skipSslValidation = skipSslValidation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
|
||||
synchronized (LOCK) {
|
||||
return super.findOne(application, profile, label, includeOrigin);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Locations getLocations(String application, String profile, String label) {
|
||||
if (label == null) {
|
||||
@@ -259,7 +274,9 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
|
||||
}
|
||||
String version;
|
||||
try {
|
||||
version = refresh(label);
|
||||
synchronized (LOCK) {
|
||||
version = refresh(label);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
|
||||
|
||||
Reference in New Issue
Block a user