diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 52a9edb..b0f7f4a 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -1193,8 +1193,14 @@ public class Agent synchronized (mediaStreams) { mediaStreams.remove(stream.getName()); - stream.free(); } + /* + * XXX The invocation of IceMediaStream#free() on stream has been moved + * out of the synchronized block in order to reduce the chances of a + * deadlock. There was no obvious reason why it should stay in the + * synchronized block at the time of the modification. + */ + stream.free(); } /** diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index 672318b..b13c9ed 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -460,16 +460,16 @@ class ConnectivityCheckServer */ public byte[] getRemoteKey(String username, String media) { - //support both the case where username is the local fragment or the - //entire user name. - int colon = username.indexOf(":"); - IceMediaStream stream = parentAgent.getStream(media); if(stream == null) { return null; } + //support both the case where username is the local fragment or the + //entire user name. + int colon = username.indexOf(":"); + if (colon < 0) { //caller gave us a ufrag diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 1c4cd50..2dff3a1 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -292,15 +292,13 @@ public class IceMediaStream { synchronized (components) { - Iterator> cmpEntries - = components.entrySet().iterator(); - - while (cmpEntries.hasNext()) + for (Iterator i = components.values().iterator(); + i.hasNext();) { - Component component = cmpEntries.next().getValue(); + Component component = i.next(); + i.remove(); component.free(); - cmpEntries.remove(); } } } diff --git a/src/org/ice4j/security/CredentialsManager.java b/src/org/ice4j/security/CredentialsManager.java index b44c0a7..322519f 100644 --- a/src/org/ice4j/security/CredentialsManager.java +++ b/src/org/ice4j/security/CredentialsManager.java @@ -22,6 +22,7 @@ import java.util.*; * authority. * * @author Emil Ivov + * @author Lyubomir Marinov */ public class CredentialsManager { @@ -32,6 +33,61 @@ public class CredentialsManager private final List authorities = new LinkedList(); + /** + * The list of CredentialsAuthoritys registered with this manager + * as being able to provide credentials. If non-null, represents + * an unmodifiable view of {@link #authorities}. The field was introduced in + * order to reduce the scopes of the synchronization blocks of + * CredentialsManager and to thus reduce the risks of deadlocks. + */ + private CredentialsAuthority[] unmodifiableAuthorities; + + /** + * Verifies whether username is currently known to any of the + * {@link CredentialsAuthority}s registered with this manager and + * and returns true if so. Returns false otherwise. + * + * @param username the user name whose validity we'd like to check. + * + * @return true if username is known to any of the + * CredentialsAuthoritys registered here and false + * otherwise. + */ + public boolean checkLocalUserName(String username) + { + for (CredentialsAuthority auth : getAuthorities()) + { + if (auth.checkLocalUserName(username)) + return true; + } + return false; + } + + /** + * Gets the list of CredentialsAuthoritys registered with this + * manager as being able to provide credentials. Warning: the + * returned value is an internal state of this instance and is to be + * considered unmodifiable. + * + * @return the list of CredentialsAuthoritys registered with this + * manager as being able to provide credentials. Warning: the + * returned value is an internal state of this instance and is to be + * considered unmodifiable. + */ + private CredentialsAuthority[] getAuthorities() + { + synchronized (authorities) + { + if (unmodifiableAuthorities == null) + { + unmodifiableAuthorities + = authorities.toArray( + new CredentialsAuthority[authorities.size()]); + } + return unmodifiableAuthorities; + } + } + /** * Queries all currently registered {@link CredentialsAuthority}s for a * password corresponding to the specified local username or user @@ -47,17 +103,12 @@ public class CredentialsManager */ public byte[] getLocalKey(String username) { - synchronized(authorities) + for (CredentialsAuthority auth : getAuthorities()) { - for (CredentialsAuthority auth : authorities) - { - byte[] passwd = auth.getLocalKey(username); + byte[] passwd = auth.getLocalKey(username); - if (passwd != null) - { - return passwd; - } - } + if (passwd != null) + return passwd; } return null; } @@ -78,46 +129,19 @@ public class CredentialsManager */ public byte[] getRemoteKey(String username, String media) { - synchronized(authorities) + for (CredentialsAuthority auth : getAuthorities()) { - for (CredentialsAuthority auth : authorities) - { - byte[] passwd = auth.getRemoteKey(username, media); + byte[] passwd = auth.getRemoteKey(username, media); - if (passwd != null) - { - /** @todo: we should probably add SASLprep here.*/ - return passwd; - } + if (passwd != null) + { + /** @todo: we should probably add SASLprep here.*/ + return passwd; } } return null; } - /** - * Verifies whether username is currently known to any of the - * {@link CredentialsAuthority}s registered with this manager and - * and returns true if so. Returns false otherwise. - * - * @param username the user name whose validity we'd like to check. - * - * @return true if username is known to any of the - * CredentialsAuthoritys registered here and false - * otherwise. - */ - public boolean checkLocalUserName(String username) - { - synchronized(authorities) - { - for (CredentialsAuthority auth : authorities) - { - if( auth.checkLocalUserName(username)) - return true; - } - } - return false; - } - /** * Adds authority to the list of {@link CredentialsAuthority}s * registered with this manager. @@ -126,10 +150,10 @@ public class CredentialsManager */ public void registerAuthority(CredentialsAuthority authority) { - synchronized(authorities) + synchronized (authorities) { - if(!authorities.contains(authority)) - authorities.add(authority); + if (!authorities.contains(authority) && authorities.add(authority)) + unmodifiableAuthorities = null; } } @@ -142,9 +166,10 @@ public class CredentialsManager */ public void unregisterAuthority(CredentialsAuthority authority) { - synchronized(authorities) + synchronized (authorities) { - authorities.remove(authority); + if (authorities.remove(authority)) + unmodifiableAuthorities = null; } } } diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 26ac6bb..dbc1d33 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -385,19 +385,32 @@ class StunClientTransaction * @param waitForResponse indicates whether we should wait for the current * RTO to expire before ending the transaction or immediately terminate. */ - synchronized void cancel(boolean waitForResponse) + void cancel(boolean waitForResponse) { - this.cancelled = true; + /* + * XXX The cancelled field is initialized to false and then the one and + * only write access to it is here to set it to true. The rest of the + * code just checks whether it has become true. Consequently, there + * shouldn't be a problem if the set is outside a synchronized block. + * However, it being outside a synchronized block will decrease the risk + * of deadlocks. + */ + cancelled = true; if(!waitForResponse) - notifyAll(); + { + synchronized (this) + { + notifyAll(); + } + } } /** * Cancels the transaction. Once this method is called the transaction is * considered terminated and will stop retransmissions. */ - synchronized void cancel() + void cancel() { cancel(false); }