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;
/**
* 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.
*/
@ -249,7 +239,10 @@ class StunClientTransaction
*/
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)
{
@ -269,12 +262,12 @@ class StunClientTransaction
try
{
logger.fine("retrying STUN "
+ " tid " + transactionID
logger.fine(
"retrying STUN tid " + transactionID
+ " from " + localAddress
+ " to " + requestDestination
+ " waited " + curWaitInterval
+ " ms retrans " + (retransmissionCounter+1)
+ " ms retrans " + (retransmissionCounter + 1)
+ " of " + maxRetransmissions);
sendRequest0();
}
@ -282,9 +275,10 @@ class StunClientTransaction
{
//I wonder whether we should notify anyone that a
//retransmission has failed
logger.log(Level.INFO,
"A client tran retransmission failed",
ex);
logger.log(
Level.INFO,
"A client tran retransmission failed",
ex);
}
}
@ -374,7 +368,7 @@ class StunClientTransaction
}
catch (InterruptedException ex)
{
throw new RuntimeException(ex);
throw new RuntimeException(ex);
}
}
@ -423,17 +417,19 @@ class StunClientTransaction
*/
synchronized void handleResponse(StunMessageEvent evt)
{
logger.log(Level.FINE, "handleResponse tid " + getTransactionID());
if( !Boolean.getBoolean(StackProperties.KEEP_CRANS_AFTER_A_RESPONSE) )
this.cancel();
TransactionID transactionID = getTransactionID();
this.responseCollector.processResponse(
logger.log(Level.FINE, "handleResponse tid " + transactionID);
if( !Boolean.getBoolean(StackProperties.KEEP_CRANS_AFTER_A_RESPONSE) )
cancel();
responseCollector.processResponse(
new StunResponseEvent(
stackCallback,
evt.getRawMessage(),
(Response) evt.getMessage(),
this.request,
getTransactionID()));
request,
transactionID));
}
/**

View File

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