diff --git a/agent.go b/agent.go index aa2a1d6f..951103bd 100644 --- a/agent.go +++ b/agent.go @@ -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) diff --git a/gather.go b/gather.go index a1dc437b..bbe416d0 100644 --- a/gather.go +++ b/gather.go @@ -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()