From 7cac0e50a7fb55ca0e7fb2ae7d65a17cbff85570 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Mon, 12 Dec 2016 13:53:42 -0600 Subject: [PATCH] fix: Moves the initializedActive update to after the actual call to initializeActive(). Synchronizes access. --- .../java/org/ice4j/ice/ComponentSocket.java | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/ice4j/ice/ComponentSocket.java b/src/main/java/org/ice4j/ice/ComponentSocket.java index 171ce9f..f52b6ee 100644 --- a/src/main/java/org/ice4j/ice/ComponentSocket.java +++ b/src/main/java/org/ice4j/ice/ComponentSocket.java @@ -23,7 +23,6 @@ import org.ice4j.socket.*; import java.beans.*; import java.net.*; import java.util.*; -import java.util.concurrent.*; import org.ice4j.util.*; //Disambiguation /** @@ -64,6 +63,11 @@ public class ComponentSocket */ private boolean initializedActive = false; + /** + * Used to synchronize access to {@link #initializedActive}. + */ + private final Object initializedActiveSyncRoot = new Object(); + /** * The {@link Logger} used by {@link MergingDatagramSocket} instances. */ @@ -153,33 +157,37 @@ public class ComponentSocket } else if (IceMediaStream.PROPERTY_PAIR_NOMINATED.equals(propertyName)) { - if (initializedActive) + synchronized (initializedActiveSyncRoot) { - return; + if (initializedActive) + { + return; + } + + // Find the remote address and the correct socket to be used by + // the pair. + LocalCandidate localCandidate = pair.getLocalCandidate(); + LocalCandidate base = localCandidate.getBase(); + if (base != null) + localCandidate = base; + + TransportAddress remoteAddress = null; + RemoteCandidate remoteCandidate = pair.getRemoteCandidate(); + if (remoteCandidate != null) + { + remoteAddress = remoteCandidate.getTransportAddress(); + } + + // The local candidate may have more than one associated socket. + // Make sure we get the one for the remote address that we are + // going to use. + IceSocketWrapper socketWrapper + = localCandidate + .getCandidateIceSocketWrapper(remoteAddress); + + initializeActive(socketWrapper, remoteAddress); + initializedActive = true; } - initializedActive = true; - - // Find the remote address and the correct socket to be used by - // the pair. - LocalCandidate localCandidate = pair.getLocalCandidate(); - LocalCandidate base = localCandidate.getBase(); - if (base != null) - localCandidate = base; - - TransportAddress remoteAddress = null; - RemoteCandidate remoteCandidate = pair.getRemoteCandidate(); - if (remoteCandidate != null) - { - remoteAddress = remoteCandidate.getTransportAddress(); - } - - // The local candidate may have more than one associated socket. - // Make sure we get the one for the remote address that we are - // going to use. - IceSocketWrapper socketWrapper - = localCandidate.getCandidateIceSocketWrapper(remoteAddress); - - initializeActive(socketWrapper, remoteAddress); } }