Add checks for participant and sub-components close. (#4100)

* Add checks for participant and sub-components close.

Looks like there might be some memory leak with participant sessions not
getting closed properly. Adding checks (to be cleaned up later) to see
if there is a consistent place where things might hang.

* init with right type

* Remove unnecessary goroutine, thank you @milos-lk

* clean up
This commit is contained in:
Raja Subramanian
2025-11-24 18:07:33 +05:30
committed by GitHub
parent ffbabcc772
commit 70f6def39d
5 changed files with 117 additions and 4 deletions
+20
View File
@@ -1554,11 +1554,31 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
// Close peer connections without blocking participant Close. If peer connections are gathering candidates
// Close will block.
go func() {
var smClosed atomic.Bool
var tmClosed atomic.Bool
var mcClosed atomic.Bool
var mrClosed atomic.Bool
time.AfterFunc(time.Minute, func() { // CLOSE-DEBUG-CLEANUP
if !smClosed.Load() || !tmClosed.Load() || !mcClosed.Load() || !mrClosed.Load() {
p.params.Logger.Infow(
"participant close timeout",
"smClosed", smClosed.Load(),
"tmClosed", tmClosed.Load(),
"mcClosed", mcClosed.Load(),
"mrClosed", mrClosed.Load(),
)
}
})
p.SubscriptionManager.Close(isExpectedToResume)
smClosed.Store(true)
p.TransportManager.Close()
tmClosed.Store(true)
p.metricsCollector.Stop()
mcClosed.Store(true)
p.metricsReporter.Stop()
mrClosed.Store(true)
}()
p.dataChannelStats.Stop()
+2 -2
View File
@@ -2041,8 +2041,8 @@ func GetOtherParticipantInfo(
return pInfos
}
func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
var fields []interface{}
func connectionDetailsFields(infos []*types.ICEConnectionInfo) []any {
var fields []any
connectionType := types.ICEConnectionTypeUnknown
for _, info := range infos {
candidates := make([]string, 0, len(info.Remote)+len(info.Local))
+15
View File
@@ -104,7 +104,21 @@ func (m *SubscriptionManager) Close(isExpectedToResume bool) {
close(m.closeCh)
m.lock.Unlock()
var done atomic.Bool
var downTracksClosed atomic.Bool
time.AfterFunc(time.Minute, func() { // CLOSE-DEBUG-CLEANUP
if !done.Load() || !downTracksClosed.Load() {
m.params.Logger.Infow(
"subscription maanager close timeout",
"done", done.Load(),
"downTracksClosed", downTracksClosed.Load(),
"numSubscribedTracks", len(m.GetSubscribedTracks()),
)
}
})
<-m.doneCh
done.Store(true)
subTracks := m.GetSubscribedTracks()
downTracksToClose := make([]*sfu.DownTrack, 0, len(subTracks))
@@ -127,6 +141,7 @@ func (m *SubscriptionManager) Close(isExpectedToResume bool) {
go dt.CloseWithFlush(true)
}
}
downTracksClosed.Store(true)
}
func (m *SubscriptionManager) isClosed() bool {
+56 -2
View File
@@ -141,7 +141,7 @@ func (s signal) String() string {
type event struct {
*PCTransport
signal signal
data interface{}
data any
}
func (e event) String() string {
@@ -280,6 +280,11 @@ type PCTransport struct {
signalStateCheckTimer *time.Timer
currentOfferIceCredential string // ice user:pwd, for publish side ice restart checking
pendingRestartIceOffer *webrtc.SessionDescription
// CLOSE-DEBUG-CLEANUP
iceGatheringState atomic.Value // webrtc.ICEGatheringState
iceConnectionState atomic.Value // webrtc.ICEConnectionState
peerConnectionState atomic.Value // webrtc.PeerConnectionState
}
type TransportParams struct {
@@ -514,6 +519,9 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
connectionDetails: types.NewICEConnectionDetails(params.Transport, params.Logger),
lastNegotiate: time.Now(),
}
t.iceGatheringState.Store(webrtc.ICEGatheringStateUnknown)
t.iceConnectionState.Store(webrtc.ICEConnectionStateUnknown)
t.peerConnectionState.Store(webrtc.PeerConnectionStateUnknown)
t.localOfferId.Store(uint32(rand.Intn(1<<8) + 1))
bwe, err := t.createPeerConnection()
@@ -768,6 +776,7 @@ func (t *PCTransport) setConnectedAt(at time.Time) bool {
}
func (t *PCTransport) onICEGatheringStateChange(state webrtc.ICEGatheringState) {
t.iceGatheringState.Store(state)
t.params.Logger.Debugw("ice gathering state change", "state", state.String())
if state != webrtc.ICEGatheringStateComplete {
return
@@ -799,6 +808,7 @@ func (t *PCTransport) handleConnectionFailed(forceShortConn bool) {
}
func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState) {
t.iceConnectionState.Store(state)
t.params.Logger.Debugw("ice connection state change", "state", state.String())
switch state {
case webrtc.ICEConnectionStateConnected:
@@ -810,6 +820,7 @@ func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState
}
func (t *PCTransport) onPeerConnectionStateChange(state webrtc.PeerConnectionState) {
t.peerConnectionState.Store(state)
t.params.Logger.Debugw("peer connection state change", "state", state.String())
switch state {
case webrtc.PeerConnectionStateConnected:
@@ -1437,15 +1448,54 @@ func (t *PCTransport) Close() {
return
}
var eventsQueueDone atomic.Bool
var streamAllocatorStopped atomic.Bool
var pacerStopped atomic.Bool
var reliableDataChannelClosed atomic.Bool
var lossyDataChannelClosed atomic.Bool
var unlabeledDataChannelClosed atomic.Bool
var peerConnectionClosed atomic.Bool
time.AfterFunc(time.Minute, func() { // CLOSE-DEBUG-CLEANUP
if !eventsQueueDone.Load() || !streamAllocatorStopped.Load() || !pacerStopped.Load() || !reliableDataChannelClosed.Load() || !lossyDataChannelClosed.Load() || !unlabeledDataChannelClosed.Load() || !peerConnectionClosed.Load() {
t.lock.Lock()
iceStartedAt := t.iceStartedAt
iceConnectedAt := t.iceConnectedAt
iceStats := t.mayFailedICEStats
t.lock.Unlock()
t.params.Logger.Infow(
"transport close timeout",
"eventsQueueDone", eventsQueueDone.Load(),
"streamAllocatorStopped", streamAllocatorStopped.Load(),
"pacerStopped", pacerStopped.Load(),
"reliableDataChannelClosed", reliableDataChannelClosed.Load(),
"lossyDataChannelClosed", lossyDataChannelClosed.Load(),
"unlabeledDataChannelClosed", unlabeledDataChannelClosed.Load(),
"peerConnectionClosed", peerConnectionClosed.Load(),
"iceStartedAt", iceStartedAt,
"iceConnectedAt", iceConnectedAt,
"iceGatheringState", t.iceGatheringState.Load().(webrtc.ICEGatheringState).String(),
"iceConnectionState", t.iceConnectionState.Load().(webrtc.ICEConnectionState).String(),
"peerConnectionState", t.peerConnectionState.Load().(webrtc.PeerConnectionState).String(),
"iceStats", iceCandidatePairStatsEncoder{iceStats},
"iceConnectionInfo", t.GetICEConnectionInfo(),
"connectionType", t.connectionDetails.GetConnectionType(),
)
}
})
<-t.eventsQueue.Stop()
eventsQueueDone.Store(true)
t.clearSignalStateCheckTimer()
if t.streamAllocator != nil {
t.streamAllocator.Stop()
}
streamAllocatorStopped.Store(true)
if t.pacer != nil {
t.pacer.Stop()
}
pacerStopped.Store(true)
t.clearConnTimer()
@@ -1459,19 +1509,23 @@ func (t *PCTransport) Close() {
t.reliableDC.Close()
t.reliableDC = nil
}
reliableDataChannelClosed.Store(true)
if t.lossyDC != nil {
t.lossyDC.Close()
t.lossyDC = nil
}
lossyDataChannelClosed.Store(true)
for _, dc := range t.unlabeledDataChannels {
dc.Close()
}
t.unlabeledDataChannels = nil
unlabeledDataChannelClosed.Store(true)
t.lock.Unlock()
_ = t.pc.Close()
peerConnectionClosed.Store(true)
t.outputAndClearICEStats()
}
@@ -2873,7 +2927,7 @@ func (t *PCTransport) handleICERestart(_ event) error {
}
func (t *PCTransport) onNegotiationFailed(warning bool, reason string) {
logFields := []interface{}{
logFields := []any{
"reason", reason,
"localCurrent", t.pc.CurrentLocalDescription(),
"localPending", t.pc.PendingLocalDescription(),
+24
View File
@@ -199,16 +199,40 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
}
func (t *TransportManager) Close() {
var publisherClosed atomic.Bool
var subscriberClosed atomic.Bool
time.AfterFunc(time.Minute, func() { // CLOSE-DEBUG-CLEANUP
if !publisherClosed.Load() || !subscriberClosed.Load() {
t.params.Logger.Infow(
"transport maanager close timeout",
"publisherClosed", publisherClosed.Load(),
"subscriberClosed", subscriberClosed.Load(),
)
}
})
if t.publisher != nil {
t.publisher.Close()
}
publisherClosed.Store(true)
if t.subscriber != nil {
t.subscriber.Close()
}
subscriberClosed.Store(true)
}
func (t *TransportManager) SubscriberClose() {
var subscriberClosed atomic.Bool
time.AfterFunc(time.Minute, func() { // CLOSE-DEBUG-CLEANUP
if !subscriberClosed.Load() {
t.params.Logger.Infow(
"transport maanager subscriber close timeout",
"subscriberClosed", subscriberClosed.Load(),
)
}
})
t.subscriber.Close()
subscriberClosed.Store(true)
}
func (t *TransportManager) HasPublisherEverConnected() bool {