Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConcurrentModificationException in GenericWebSocketClient. #12654

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nonnull;
import lombok.Builder;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.triplea.http.client.web.socket.messages.MessageType;
import org.triplea.http.client.web.socket.messages.WebSocketMessage;
Expand Down Expand Up @@ -45,8 +45,7 @@ public class GenericWebSocketClient implements WebSocket, WebSocketConnectionLis
private final Function<URI, WebSocketConnection> webSocketConnectionFactory;

private WebSocketConnection webSocketConnection;
private final Set<MessageListener<? extends WebSocketMessage>> listeners =
Collections.synchronizedSet(new HashSet<>());
private final Set<MessageListener<? extends WebSocketMessage>> listeners = new HashSet<>();

@Builder
private static class MessageListener<T extends WebSocketMessage> {
Expand Down Expand Up @@ -98,6 +97,7 @@ public void close() {

@SuppressWarnings("unchecked")
@Override
@Synchronized
public <T extends WebSocketMessage> void addListener(
final MessageType<T> messageType, final Consumer<T> messageHandler) {

Expand Down Expand Up @@ -125,15 +125,14 @@ public void addConnectionTerminatedListener(final Consumer<String> connectionTer
}

@Override
@Synchronized
public void messageReceived(final String message) {
final MessageEnvelope converted = gson.fromJson(message, MessageEnvelope.class);

listeners.stream()
.filter(listener -> converted.messageTypeIs(listener.messageType))
.forEach(
listener ->
listener.listener.accept(
converted.getPayload(listener.messageType.getPayloadType())));
for (var listener : listeners) {
if (converted.messageTypeIs(listener.messageType)) {
listener.listener.accept(converted.getPayload(listener.messageType.getPayloadType()));
}
}
}

@Override
Expand Down