Fix thread leak when Agent is freed before termination timer triggered. (#150)
* Fix thread leak when Agent is freed before termination timer triggered. * Use ScheduledExecutorService to terminate agent with timeout * Fixed formatting to match project code style.
This commit is contained in:
parent
7d2ad8f9a2
commit
ba7e30c189
@ -23,6 +23,7 @@ import java.math.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
@ -114,6 +115,38 @@ public class Agent
|
|||||||
public static final String PROPERTY_ICE_PROCESSING_STATE
|
public static final String PROPERTY_ICE_PROCESSING_STATE
|
||||||
= "IceProcessingState";
|
= "IceProcessingState";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ScheduledExecutorService to execute planned agent termination
|
||||||
|
*/
|
||||||
|
private static final ScheduledExecutorService agentTerminationScheduler;
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
final ScheduledThreadPoolExecutor terminationExecutor
|
||||||
|
= new ScheduledThreadPoolExecutor(0);
|
||||||
|
terminationExecutor.setKeepAliveTime(10, TimeUnit.SECONDS);
|
||||||
|
terminationExecutor.setRemoveOnCancelPolicy(true);
|
||||||
|
agentTerminationScheduler
|
||||||
|
= Executors.unconfigurableScheduledExecutorService(
|
||||||
|
terminationExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Termination task which will be scheduled with timeout
|
||||||
|
*/
|
||||||
|
private final Runnable terminationRunnable = new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
terminate(IceProcessingState.TERMINATED);
|
||||||
|
synchronized (terminationFutureSyncRoot)
|
||||||
|
{
|
||||||
|
terminationFuture = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The LinkedHashMap used to store the media streams
|
* The LinkedHashMap used to store the media streams
|
||||||
* This map preserves the insertion order of the media streams.
|
* This map preserves the insertion order of the media streams.
|
||||||
@ -246,14 +279,14 @@ public class Agent
|
|||||||
private StunStack stunStack;
|
private StunStack stunStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The thread that we use for moving from COMPLETED into a TERMINATED state.
|
* The scheduled task to terminate this <tt>Agent</tt>
|
||||||
*/
|
*/
|
||||||
private TerminationThread terminationThread;
|
private ScheduledFuture<?> terminationFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The object used to synchronize access to {@link #terminationThread}.
|
* The object used to synchronize access to {@link #terminationFuture}.
|
||||||
*/
|
*/
|
||||||
private final Object terminationThreadSyncRoot = new Object();
|
private final Object terminationFutureSyncRoot = new Object();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The thread that we use for STUN keep-alive.
|
* The thread that we use for STUN keep-alive.
|
||||||
@ -2150,18 +2183,49 @@ public class Agent
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes and starts the {@link TerminationThread}
|
* Initializes and starts the {@link #terminationFuture}
|
||||||
*/
|
*/
|
||||||
private void scheduleTermination()
|
private void scheduleTermination()
|
||||||
{
|
{
|
||||||
synchronized (terminationThreadSyncRoot)
|
/*
|
||||||
|
* RFC 5245 says: Once ICE processing has reached the Completed state for
|
||||||
|
* all peers for media streams using those candidates, the agent SHOULD
|
||||||
|
* wait an additional three seconds, and then it MAY cease responding to
|
||||||
|
* checks or generating triggered checks on that candidate. It MAY free
|
||||||
|
* the candidate at that time.
|
||||||
|
* <p>
|
||||||
|
* This method is scheduling such a termination.
|
||||||
|
*/
|
||||||
|
boolean runTerminationImmediately = false;
|
||||||
|
|
||||||
|
synchronized (terminationFutureSyncRoot)
|
||||||
{
|
{
|
||||||
if (terminationThread == null)
|
if (terminationFuture == null)
|
||||||
{
|
{
|
||||||
terminationThread = new TerminationThread();
|
long terminationDelay
|
||||||
terminationThread.start();
|
= Integer.getInteger(
|
||||||
|
StackProperties.TERMINATION_DELAY,
|
||||||
|
DEFAULT_TERMINATION_DELAY);
|
||||||
|
|
||||||
|
if (terminationDelay > 0)
|
||||||
|
{
|
||||||
|
terminationFuture
|
||||||
|
= agentTerminationScheduler.schedule(
|
||||||
|
terminationRunnable,
|
||||||
|
terminationDelay,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
runTerminationImmediately = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (runTerminationImmediately)
|
||||||
|
{
|
||||||
|
terminationRunnable.run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2298,6 +2362,17 @@ public class Agent
|
|||||||
if (stunKeepAliveThread != null)
|
if (stunKeepAliveThread != null)
|
||||||
stunKeepAliveThread.interrupt();
|
stunKeepAliveThread.interrupt();
|
||||||
|
|
||||||
|
// cancel termination timer in case agent is freed
|
||||||
|
// before termination timer is triggered
|
||||||
|
synchronized (terminationFutureSyncRoot)
|
||||||
|
{
|
||||||
|
if (terminationFuture != null)
|
||||||
|
{
|
||||||
|
terminationFuture.cancel(true);
|
||||||
|
terminationFuture = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//stop responding to STUN Binding Requests.
|
//stop responding to STUN Binding Requests.
|
||||||
connCheckServer.stop();
|
connCheckServer.stop();
|
||||||
|
|
||||||
@ -2709,62 +2784,4 @@ public class Agent
|
|||||||
{
|
{
|
||||||
return logger;
|
return logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* RFC 5245 says: Once ICE processing has reached the Completed state for
|
|
||||||
* all peers for media streams using those candidates, the agent SHOULD
|
|
||||||
* wait an additional three seconds, and then it MAY cease responding to
|
|
||||||
* checks or generating triggered checks on that candidate. It MAY free
|
|
||||||
* the candidate at that time.
|
|
||||||
* <p>
|
|
||||||
* This <tt>TerminationThread</tt> is scheduling such a termination and
|
|
||||||
* garbage collection in three seconds.
|
|
||||||
*/
|
|
||||||
private class TerminationThread
|
|
||||||
extends Thread
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new termination timer.
|
|
||||||
*/
|
|
||||||
private TerminationThread()
|
|
||||||
{
|
|
||||||
super("TerminationThread");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Waits for a period of three seconds (or whatever termination
|
|
||||||
* interval the user has specified) and then moves this <tt>Agent</tt>
|
|
||||||
* into the terminated state and frees all non-nominated candidates.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void run()
|
|
||||||
{
|
|
||||||
long terminationDelay
|
|
||||||
= Integer.getInteger(
|
|
||||||
StackProperties.TERMINATION_DELAY,
|
|
||||||
DEFAULT_TERMINATION_DELAY);
|
|
||||||
|
|
||||||
if (terminationDelay >= 0)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
wait(terminationDelay);
|
|
||||||
}
|
|
||||||
catch (InterruptedException ie)
|
|
||||||
{
|
|
||||||
logger.log(Level.FINEST, "Interrupted while waiting. Will "
|
|
||||||
+ "speed up termination",
|
|
||||||
ie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
terminate(IceProcessingState.TERMINATED);
|
|
||||||
|
|
||||||
synchronized (terminationThreadSyncRoot)
|
|
||||||
{
|
|
||||||
terminationThread = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user