Fix panics in ICEGatherer when it's already closed

ICEGatherer throws panics in GetLocalCandidates(), GetLocalParameters()
when it is already closed. Fix them.

Resolves pion#2261
This commit is contained in:
Yoon SeungYong
2022-09-29 16:25:19 +09:00
committed by David Zhao
parent 81376b2ca8
commit 9509f736be
2 changed files with 71 additions and 6 deletions

View File

@@ -142,10 +142,7 @@ func (g *ICEGatherer) Gather() error {
return err
}
g.lock.Lock()
agent := g.agent
g.lock.Unlock()
agent := g.getAgent()
// it is possible agent had just been closed
if agent == nil {
return fmt.Errorf("%w: unable to gather", errICEAgentNotExist)
@@ -205,7 +202,13 @@ func (g *ICEGatherer) GetLocalParameters() (ICEParameters, error) {
return ICEParameters{}, err
}
frag, pwd, err := g.agent.GetLocalUserCredentials()
agent := g.getAgent()
// it is possible agent had just been closed
if agent == nil {
return ICEParameters{}, fmt.Errorf("%w: unable to get local parameters", errICEAgentNotExist)
}
frag, pwd, err := agent.GetLocalUserCredentials()
if err != nil {
return ICEParameters{}, err
}
@@ -222,7 +225,14 @@ func (g *ICEGatherer) GetLocalCandidates() ([]ICECandidate, error) {
if err := g.createAgent(); err != nil {
return nil, err
}
iceCandidates, err := g.agent.GetLocalCandidates()
agent := g.getAgent()
// it is possible agent had just been closed
if agent == nil {
return nil, fmt.Errorf("%w: unable to get local candidates", errICEAgentNotExist)
}
iceCandidates, err := agent.GetLocalCandidates()
if err != nil {
return nil, err
}

View File

@@ -98,3 +98,58 @@ func TestICEGather_mDNSCandidateGathering(t *testing.T) {
<-gotMulticastDNSCandidate.Done()
assert.NoError(t, gatherer.Close())
}
func TestICEGatherer_AlreadyClosed(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
opts := ICEGatherOptions{
ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
}
t.Run("Gather", func(t *testing.T) {
gatherer, err := NewAPI().NewICEGatherer(opts)
assert.NoError(t, err)
err = gatherer.createAgent()
assert.NoError(t, err)
err = gatherer.Close()
assert.NoError(t, err)
err = gatherer.Gather()
assert.ErrorIs(t, err, errICEAgentNotExist)
})
t.Run("GetLocalParameters", func(t *testing.T) {
gatherer, err := NewAPI().NewICEGatherer(opts)
assert.NoError(t, err)
err = gatherer.createAgent()
assert.NoError(t, err)
err = gatherer.Close()
assert.NoError(t, err)
_, err = gatherer.GetLocalParameters()
assert.ErrorIs(t, err, errICEAgentNotExist)
})
t.Run("GetLocalCandidates", func(t *testing.T) {
gatherer, err := NewAPI().NewICEGatherer(opts)
assert.NoError(t, err)
err = gatherer.createAgent()
assert.NoError(t, err)
err = gatherer.Close()
assert.NoError(t, err)
_, err = gatherer.GetLocalCandidates()
assert.ErrorIs(t, err, errICEAgentNotExist)
})
}