Skip to content

Commit 956f570

Browse files
authored
Avoid race conditions and multiple agent discovery feature states (#9135)
1 parent 89e92d5 commit 956f570

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

communication/src/main/java/datadog/communication/ddagent/SharedCommunicationObjects.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SharedCommunicationObjects {
3030
public OkHttpClient okHttpClient;
3131
public HttpUrl agentUrl;
3232
public Monitoring monitoring;
33-
private DDAgentFeaturesDiscovery featuresDiscovery;
33+
private volatile DDAgentFeaturesDiscovery featuresDiscovery;
3434
private ConfigurationPoller configurationPoller;
3535

3636
public SharedCommunicationObjects() {
@@ -139,28 +139,34 @@ public void setFeaturesDiscovery(DDAgentFeaturesDiscovery featuresDiscovery) {
139139
}
140140

141141
public DDAgentFeaturesDiscovery featuresDiscovery(Config config) {
142-
if (featuresDiscovery == null) {
143-
createRemaining(config);
144-
featuresDiscovery =
145-
new DDAgentFeaturesDiscovery(
146-
okHttpClient,
147-
monitoring,
148-
agentUrl,
149-
config.isTraceAgentV05Enabled(),
150-
config.isTracerMetricsEnabled());
151-
152-
if (paused) {
153-
// defer remote discovery until remote I/O is allowed
154-
} else {
155-
if (AGENT_THREAD_GROUP.equals(Thread.currentThread().getThreadGroup())) {
156-
featuresDiscovery.discover(); // safe to run on same thread
157-
} else {
158-
// avoid performing blocking I/O operation on application thread
159-
AgentTaskScheduler.INSTANCE.execute(featuresDiscovery::discover);
142+
DDAgentFeaturesDiscovery ret = featuresDiscovery;
143+
if (ret == null) {
144+
synchronized (this) {
145+
if (featuresDiscovery == null) {
146+
createRemaining(config);
147+
ret =
148+
new DDAgentFeaturesDiscovery(
149+
okHttpClient,
150+
monitoring,
151+
agentUrl,
152+
config.isTraceAgentV05Enabled(),
153+
config.isTracerMetricsEnabled());
154+
155+
if (paused) {
156+
// defer remote discovery until remote I/O is allowed
157+
} else {
158+
if (AGENT_THREAD_GROUP.equals(Thread.currentThread().getThreadGroup())) {
159+
ret.discover(); // safe to run on same thread
160+
} else {
161+
// avoid performing blocking I/O operation on application thread
162+
AgentTaskScheduler.INSTANCE.execute(ret::discover);
163+
}
164+
}
165+
featuresDiscovery = ret;
160166
}
161167
}
162168
}
163-
return featuresDiscovery;
169+
return ret;
164170
}
165171

166172
private static final class FixedConfigUrlSupplier implements Supplier<String> {

0 commit comments

Comments
 (0)