Fix Ticker leak

time.Ticker leaks if not Stop()-ed after use.
Close connectivity interval ticker on Agent.Close() to fix leak.
This commit is contained in:
Atsushi Watanabe
2019-08-01 07:21:42 -07:00
committed by Sean DuBois
parent ecaa44d88f
commit 0f5c553070
2 changed files with 13 additions and 5 deletions
+8 -5
View File
@@ -79,8 +79,8 @@ type Agent struct {
onConnected chan struct{}
onConnectedOnce sync.Once
connectivityChan <-chan time.Time
// force candidate to be contacted immediately (instead of waiting for connectivityChan)
connectivityTicker *time.Ticker
// force candidate to be contacted immediately (instead of waiting for connectivityTicker)
forceCandidateContact chan bool
trickle bool
@@ -462,8 +462,7 @@ func (a *Agent) startConnectivityChecks(isControlling bool, remoteUfrag, remoteP
// TODO this should be dynamic, and grow when the connection is stable
agent.forceCandidateContact <- true
t := time.NewTicker(a.taskLoopInterval)
agent.connectivityChan = t.C
agent.connectivityTicker = time.NewTicker(a.taskLoopInterval)
})
}
@@ -586,7 +585,7 @@ func (a *Agent) taskLoop() {
select {
case <-a.forceCandidateContact:
a.selector.ContactCandidates()
case <-a.connectivityChan:
case <-a.connectivityTicker.C:
a.selector.ContactCandidates()
case t := <-a.taskChan:
// Run the task
@@ -787,6 +786,10 @@ func (a *Agent) Close() error {
a.log.Warnf("failed to close buffer: %v", err)
}
if a.connectivityTicker != nil {
a.connectivityTicker.Stop()
}
a.closeMulticastConn()
})
if err != nil {
+5
View File
@@ -222,6 +222,7 @@ func TestHandlePeerReflexive(t *testing.T) {
var config AgentConfig
runAgentTest(t, &config, func(a *Agent) {
a.selector = &controllingSelector{agent: a, log: a.log}
a.connectivityTicker = time.NewTicker(a.taskLoopInterval)
hostConfig := CandidateHostConfig{
Network: "udp",
@@ -287,6 +288,7 @@ func TestHandlePeerReflexive(t *testing.T) {
var config AgentConfig
runAgentTest(t, &config, func(a *Agent) {
a.selector = &controllingSelector{agent: a, log: a.log}
a.connectivityTicker = time.NewTicker(a.taskLoopInterval)
hostConfig := CandidateHostConfig{
Network: "tcp",
@@ -318,6 +320,7 @@ func TestHandlePeerReflexive(t *testing.T) {
var config AgentConfig
runAgentTest(t, &config, func(a *Agent) {
a.selector = &controllingSelector{agent: a, log: a.log}
a.connectivityTicker = time.NewTicker(a.taskLoopInterval)
tID := [stun.TransactionIDSize]byte{}
copy(tID[:], []byte("ABC"))
a.pendingBindingRequests = []bindingRequest{
@@ -470,6 +473,7 @@ func TestInboundValidity(t *testing.T) {
err = a.run(func(a *Agent) {
a.selector = &controllingSelector{agent: a, log: a.log}
a.connectivityTicker = time.NewTicker(a.taskLoopInterval)
a.handleInbound(buildMsg(stun.ClassRequest, a.localUfrag+":"+a.remoteUfrag, a.localPwd), local, remote)
if len(a.remoteCandidates) != 1 {
t.Fatal("Binding with valid values was unable to create prflx candidate")
@@ -485,6 +489,7 @@ func TestInboundValidity(t *testing.T) {
var config AgentConfig
runAgentTest(t, &config, func(a *Agent) {
a.selector = &controllingSelector{agent: a, log: a.log}
a.connectivityTicker = time.NewTicker(a.taskLoopInterval)
msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
stun.NewUsername(a.localUfrag+":"+a.remoteUfrag),
stun.NewShortTermIntegrity(a.localPwd),