Skip to content

Commit ba37adc

Browse files
committed
Synchronizing access to hasPendingRequest
1 parent ac84012 commit ba37adc

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

opamp-client/src/main/java/io/opentelemetry/opamp/client/internal/request/service/WebSocketRequestService.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.function.Supplier;
2424
import javax.annotation.Nonnull;
2525
import javax.annotation.Nullable;
26+
import javax.annotation.concurrent.GuardedBy;
2627
import opamp.proto.ServerErrorResponse;
2728
import opamp.proto.ServerErrorResponseType;
2829
import opamp.proto.ServerToAgent;
@@ -32,12 +33,16 @@ public final class WebSocketRequestService implements RequestService, WebSocket.
3233
private final PeriodicDelay periodicRetryDelay;
3334
private final AtomicBoolean retryingConnection = new AtomicBoolean(false);
3435
private final AtomicBoolean nextRetryScheduled = new AtomicBoolean(false);
35-
private final AtomicBoolean hasPendingRequest = new AtomicBoolean(false);
3636
private final AtomicBoolean isRunning = new AtomicBoolean(false);
3737
private final AtomicBoolean hasStopped = new AtomicBoolean(false);
3838
private final ScheduledExecutorService executorService;
3939
public static final PeriodicDelay DEFAULT_DELAY_BETWEEN_RETRIES =
4040
PeriodicDelay.ofFixedDuration(Duration.ofSeconds(30));
41+
42+
@GuardedBy("hasPendingRequestLock")
43+
private boolean hasPendingRequest = false;
44+
45+
private final Object hasPendingRequestLock = new Object();
4146
@Nullable private Callback callback;
4247
@Nullable private Supplier<Request> requestSupplier;
4348

@@ -103,8 +108,10 @@ public void sendRequest() {
103108

104109
private void doSendRequest() {
105110
try {
106-
if (!trySendRequest()) {
107-
hasPendingRequest.set(true);
111+
synchronized (hasPendingRequestLock) {
112+
if (!trySendRequest()) {
113+
hasPendingRequest = true;
114+
}
108115
}
109116
} catch (IOException e) {
110117
getCallback().onRequestFailed(e);
@@ -138,8 +145,11 @@ public void stop() {
138145
public void onOpen() {
139146
retryingConnection.set(false);
140147
getCallback().onConnectionSuccess();
141-
if (hasPendingRequest.compareAndSet(true, false)) {
142-
sendRequest();
148+
synchronized (hasPendingRequestLock) {
149+
if (hasPendingRequest) {
150+
hasPendingRequest = false;
151+
sendRequest();
152+
}
143153
}
144154
}
145155

0 commit comments

Comments
 (0)