mirror of
https://github.com/netbirdio/ice.git
synced 2026-05-22 17:10:58 -07:00
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:
committed by
Eric Daniels
parent
57010dcf60
commit
427ac0fddb
@@ -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)
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user