Fixes 'Deadlock in StunStack/StunClientTransaction' reported by Carl Hasselskog.

This commit is contained in:
lubomir.marinov 2014-05-27 21:22:50 +00:00
parent 3dc113edd8
commit ed918505b8
2 changed files with 86 additions and 49 deletions

View File

@ -122,16 +122,6 @@ class StunClientTransaction
*/ */
public int maxWaitInterval = DEFAULT_MAX_WAIT_INTERVAL; public int maxWaitInterval = DEFAULT_MAX_WAIT_INTERVAL;
/**
* Indicates how many times we have retransmitted so fat.
*/
private int retransmissionCounter = 0;
/**
* How much did we wait after our last retransmission.
*/
private int nextWaitInterval = originalWaitInterval;
/** /**
* The <tt>StunStack</tt> that created us. * The <tt>StunStack</tt> that created us.
*/ */
@ -249,7 +239,10 @@ class StunClientTransaction
*/ */
public void run() public void run()
{ {
nextWaitInterval = originalWaitInterval; // Indicates how many times we have retransmitted so far.
int retransmissionCounter = 0;
// How much did we wait after our last retransmission?
int nextWaitInterval = originalWaitInterval;
synchronized(this) synchronized(this)
{ {
@ -269,12 +262,12 @@ class StunClientTransaction
try try
{ {
logger.fine("retrying STUN " logger.fine(
+ " tid " + transactionID "retrying STUN tid " + transactionID
+ " from " + localAddress + " from " + localAddress
+ " to " + requestDestination + " to " + requestDestination
+ " waited " + curWaitInterval + " waited " + curWaitInterval
+ " ms retrans " + (retransmissionCounter+1) + " ms retrans " + (retransmissionCounter + 1)
+ " of " + maxRetransmissions); + " of " + maxRetransmissions);
sendRequest0(); sendRequest0();
} }
@ -282,9 +275,10 @@ class StunClientTransaction
{ {
//I wonder whether we should notify anyone that a //I wonder whether we should notify anyone that a
//retransmission has failed //retransmission has failed
logger.log(Level.INFO, logger.log(
"A client tran retransmission failed", Level.INFO,
ex); "A client tran retransmission failed",
ex);
} }
} }
@ -374,7 +368,7 @@ class StunClientTransaction
} }
catch (InterruptedException ex) catch (InterruptedException ex)
{ {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
@ -423,17 +417,19 @@ class StunClientTransaction
*/ */
synchronized void handleResponse(StunMessageEvent evt) synchronized void handleResponse(StunMessageEvent evt)
{ {
logger.log(Level.FINE, "handleResponse tid " + getTransactionID()); TransactionID transactionID = getTransactionID();
if( !Boolean.getBoolean(StackProperties.KEEP_CRANS_AFTER_A_RESPONSE) )
this.cancel();
this.responseCollector.processResponse( logger.log(Level.FINE, "handleResponse tid " + transactionID);
if( !Boolean.getBoolean(StackProperties.KEEP_CRANS_AFTER_A_RESPONSE) )
cancel();
responseCollector.processResponse(
new StunResponseEvent( new StunResponseEvent(
stackCallback, stackCallback,
evt.getRawMessage(), evt.getRawMessage(),
(Response) evt.getMessage(), (Response) evt.getMessage(),
this.request, request,
getTransactionID())); transactionID));
} }
/** /**

View File

@ -241,6 +241,8 @@ public class StunStack
*/ */
private void cancelTransactionsForAddress(TransportAddress localAddr) private void cancelTransactionsForAddress(TransportAddress localAddr)
{ {
List<StunClientTransaction> clientTransactionsToCancel = null;
synchronized (clientTransactions) synchronized (clientTransactions)
{ {
Iterator<StunClientTransaction> clientTransactionsIter Iterator<StunClientTransaction> clientTransactionsIter
@ -253,10 +255,33 @@ public class StunStack
if (tran.getLocalAddress().equals(localAddr)) if (tran.getLocalAddress().equals(localAddr))
{ {
clientTransactionsIter.remove(); clientTransactionsIter.remove();
tran.cancel();
/*
* Invoke StunClientTransaction.cancel() outside the
* clientTransactions-synchronized block in order to avoid a
* deadlock. Reported by Carl Hasselskog.
*/
if (clientTransactionsToCancel == null)
{
clientTransactionsToCancel
= new LinkedList<StunClientTransaction>();
}
clientTransactionsToCancel.add(tran);
} }
} }
} }
/*
* Invoke StunClientTransaction.cancel() outside the
* clientTransactions-synchronized block in order to avoid a deadlock.
* Reported by Carl Hasselskog.
*/
if (clientTransactionsToCancel != null)
{
for (StunClientTransaction tran : clientTransactionsToCancel)
tran.cancel();
}
List<StunServerTransaction> serverTransactionsToExpire = null;
synchronized (serverTransactions) synchronized (serverTransactions)
{ {
@ -274,10 +299,21 @@ public class StunStack
&& sendingAddr.equals(localAddr))) && sendingAddr.equals(localAddr)))
{ {
serverTransactionsIter.remove(); serverTransactionsIter.remove();
tran.expire();
if (serverTransactionsToExpire == null)
{
serverTransactionsToExpire
= new LinkedList<StunServerTransaction>();
}
serverTransactionsToExpire.add(tran);
} }
} }
} }
if (serverTransactionsToExpire != null)
{
for (StunServerTransaction tran : serverTransactionsToExpire)
tran.expire();
}
} }
/** /**
@ -388,7 +424,7 @@ public class StunStack
public TransactionID sendRequest( Request request, public TransactionID sendRequest( Request request,
TransportAddress sendTo, TransportAddress sendTo,
TransportAddress sendThrough, TransportAddress sendThrough,
ResponseCollector collector ) ResponseCollector collector)
throws IOException, IllegalArgumentException throws IOException, IllegalArgumentException
{ {
return sendRequest(request, sendTo, sendThrough, collector, return sendRequest(request, sendTo, sendThrough, collector,
@ -658,8 +694,10 @@ public class StunStack
} }
/** /**
* Removes a client transaction from this providers client transactions list. * Removes a client transaction from this providers client transactions
* Method is used by StunClientTransaction-s themselves when a timeout occurs. * list. The method is used by <tt>StunClientTransaction</tt>s themselves
* when a timeout occurs.
*
* @param tran the transaction to remove. * @param tran the transaction to remove.
*/ */
void removeClientTransaction(StunClientTransaction tran) void removeClientTransaction(StunClientTransaction tran)
@ -876,33 +914,36 @@ public class StunStack
{ {
eventDispatcher.removeAllListeners(); eventDispatcher.removeAllListeners();
// clientTransactions
Collection<StunClientTransaction> clientTransactionsToCancel;
synchronized (clientTransactions) synchronized (clientTransactions)
{ {
for (Iterator<StunClientTransaction> i clientTransactionsToCancel
= clientTransactions.values().iterator(); = new ArrayList<StunClientTransaction>(
i.hasNext();) clientTransactions.values());
{ clientTransactions.clear();
StunClientTransaction tran = i.next();
i.remove();
if (tran != null)
tran.cancel();
}
} }
/*
* Invoke StunClientTransaction.cancel() outside the
* clientTransactions-synchronized block in order to avoid a deadlock.
* Reported by Carl Hasselskog.
*/
for (StunClientTransaction tran : clientTransactionsToCancel)
tran.cancel();
// serverTransactions
Collection<StunServerTransaction> serverTransactionsToExpire;
synchronized (serverTransactions) synchronized (serverTransactions)
{ {
for (Iterator<StunServerTransaction> i serverTransactionsToExpire
= serverTransactions.values().iterator(); = new ArrayList<StunServerTransaction>(
i.hasNext();) serverTransactions.values());
{ serverTransactions.clear();
StunServerTransaction tran = i.next();
i.remove();
if (tran != null)
tran.expire();
}
} }
for (StunServerTransaction tran : serverTransactionsToExpire)
tran.expire();
netAccessManager.stop(); netAccessManager.stop();
} }