Fix goroutine leak for closing while gathering

The "gatherCandidates()" function could previously leak after
close, because the WaitGroup was left unchecked.
This commit is contained in:
Kyle Carberry
2022-03-01 19:53:05 -05:00
committed by Eric Daniels
parent 57010dcf60
commit 427ac0fddb
2 changed files with 7 additions and 0 deletions
+4
View File
@@ -114,6 +114,7 @@ type Agent struct {
err atomicError
gatherCandidateCancel func()
gatherCandidateDone chan struct{}
chanCandidate chan Candidate
chanCandidatePair chan *CandidatePair
@@ -907,6 +908,9 @@ func (a *Agent) Close() error {
a.afterRun(func(context.Context) {
a.gatherCandidateCancel()
if a.gatherCandidateDone != nil {
<-a.gatherCandidateDone
}
})
a.err.Store(ErrClosed)
+3
View File
@@ -71,6 +71,7 @@ func (a *Agent) GatherCandidates() error {
a.gatherCandidateCancel() // Cancel previous gathering routine
ctx, cancel := context.WithCancel(ctx)
a.gatherCandidateCancel = cancel
a.gatherCandidateDone = make(chan struct{})
go a.gatherCandidates(ctx)
}); runErr != nil {
@@ -80,6 +81,7 @@ func (a *Agent) GatherCandidates() error {
}
func (a *Agent) gatherCandidates(ctx context.Context) {
defer close(a.gatherCandidateDone)
if err := a.setGatheringState(GatheringStateGathering); err != nil {
a.log.Warnf("failed to set gatheringState to GatheringStateGathering: %v", err)
return
@@ -120,6 +122,7 @@ func (a *Agent) gatherCandidates(ctx context.Context) {
case CandidateTypePeerReflexive, CandidateTypeUnspecified:
}
}
// Block until all STUN and TURN URLs have been gathered (or timed out)
wg.Wait()