From 7930dcde2599c2a99f5414c7ee0bda0b9be25636 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 15 Oct 2025 00:04:12 +0530 Subject: [PATCH] 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 --- pkg/rtc/transport.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index 3fdbe0f12..97997f975 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -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() }