Skip to content

Commit

Permalink
Add a lock to prevent race condition between EnvironmentController an…
Browse files Browse the repository at this point in the history
…d ResourceController

Fixes spring-cloud#2681
  • Loading branch information
ryanjbaxter committed Feb 4, 2025
1 parent 464135d commit d199b0d
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -252,14 +260,23 @@ public void setSkipSslValidation(boolean skipSslValidation) {
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) {
label = this.defaultLabel;
}
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)
Expand Down

0 comments on commit d199b0d

Please sign in to comment.