Fix a race stopping a connectivity check client. (#237)

* Fix a race stopping a connectivity check client.

(This could result in the agent, and everything it references, staying reachable and so being leaked.)
This commit is contained in:
Jonathan Lennox 2021-08-24 17:40:35 -04:00 committed by GitHub
parent 076281c76b
commit adca67b17f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,7 @@ import java.net.*;
import java.time.*; import java.time.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import org.ice4j.*; import org.ice4j.*;
import org.ice4j.attribute.*; import org.ice4j.attribute.*;
@ -67,6 +68,12 @@ class ConnectivityCheckClient
private final Queue<PaceMaker> paceMakers private final Queue<PaceMaker> paceMakers
= new ConcurrentLinkedQueue<>(); = new ConcurrentLinkedQueue<>();
/**
* Whether this {@link ConnectivityCheckClient} has been stopped.
* Synchronized by {@link #paceMakers}.
*/
private boolean stopped = false;
/** /**
* Timer that is used to let some seconds before a CheckList is considered * Timer that is used to let some seconds before a CheckList is considered
* as FAILED. * as FAILED.
@ -160,9 +167,16 @@ class ConnectivityCheckClient
*/ */
public void startChecks(CheckList checkList) public void startChecks(CheckList checkList)
{ {
PaceMaker paceMaker = new PaceMaker(checkList); synchronized (paceMakers)
paceMakers.add(paceMaker); {
paceMaker.schedule(); if (stopped)
{
return;
}
PaceMaker paceMaker = new PaceMaker(checkList);
paceMakers.add(paceMaker);
paceMaker.schedule();
}
} }
/** /**
@ -969,14 +983,18 @@ class ConnectivityCheckClient
*/ */
public void stop() public void stop()
{ {
while (true) synchronized (paceMakers)
{ {
final PaceMaker paceMaker = paceMakers.poll(); stopped = true;
if (paceMaker == null) while (true)
{ {
break; final PaceMaker paceMaker = paceMakers.poll();
if (paceMaker == null)
{
break;
}
paceMaker.cancel();
} }
paceMaker.cancel();
} }
} }
} }