Do not try to read stats from peer connection after close. (#4002)

Pion does not protect the stats getter and using it after close could
cause nil de-reference. Do a couple of things
1. Stop timer that access peer connection stats before closing peer
   connection.
2. Do not access stats if peer connection is already closed
This commit is contained in:
Raja Subramanian
2025-10-15 00:04:12 +05:30
committed by GitHub
parent ca0d5ee972
commit 7930dcde25
+12 -5
View File
@@ -688,6 +688,10 @@ func (t *PCTransport) setICEConnectedAt(at time.Time) {
}
func (t *PCTransport) logMayFailedICEStats() {
if t.pc.ConnectionState() == webrtc.PeerConnectionStateClosed {
return
}
var candidatePairStats []webrtc.ICECandidatePairStats
pairStats := t.pc.GetStats()
candidateStats := make(map[string]webrtc.ICECandidateStats)
@@ -1441,6 +1445,13 @@ func (t *PCTransport) Close() {
t.pacer.Stop()
}
t.lock.Lock()
if t.mayFailedICEStatsTimer != nil {
t.mayFailedICEStatsTimer.Stop()
t.mayFailedICEStatsTimer = nil
}
t.lock.Unlock()
_ = t.pc.Close()
t.clearConnTimer()
@@ -1460,12 +1471,8 @@ func (t *PCTransport) Close() {
dc.Close()
}
t.unlabeledDataChannels = nil
if t.mayFailedICEStatsTimer != nil {
t.mayFailedICEStatsTimer.Stop()
t.mayFailedICEStatsTimer = nil
}
t.lock.Unlock()
t.outputAndClearICEStats()
}