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:
@@ -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 peer connections without blocking participant Close. If peer connections are gathering candidates
|
||||||
// Close will block.
|
// Close will block.
|
||||||
go func() {
|
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)
|
p.SubscriptionManager.Close(isExpectedToResume)
|
||||||
|
smClosed.Store(true)
|
||||||
p.TransportManager.Close()
|
p.TransportManager.Close()
|
||||||
|
tmClosed.Store(true)
|
||||||
|
|
||||||
p.metricsCollector.Stop()
|
p.metricsCollector.Stop()
|
||||||
|
mcClosed.Store(true)
|
||||||
p.metricsReporter.Stop()
|
p.metricsReporter.Stop()
|
||||||
|
mrClosed.Store(true)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
p.dataChannelStats.Stop()
|
p.dataChannelStats.Stop()
|
||||||
|
|||||||
+2
-2
@@ -2041,8 +2041,8 @@ func GetOtherParticipantInfo(
|
|||||||
return pInfos
|
return pInfos
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
|
func connectionDetailsFields(infos []*types.ICEConnectionInfo) []any {
|
||||||
var fields []interface{}
|
var fields []any
|
||||||
connectionType := types.ICEConnectionTypeUnknown
|
connectionType := types.ICEConnectionTypeUnknown
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
candidates := make([]string, 0, len(info.Remote)+len(info.Local))
|
candidates := make([]string, 0, len(info.Remote)+len(info.Local))
|
||||||
|
|||||||
@@ -104,7 +104,21 @@ func (m *SubscriptionManager) Close(isExpectedToResume bool) {
|
|||||||
close(m.closeCh)
|
close(m.closeCh)
|
||||||
m.lock.Unlock()
|
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
|
<-m.doneCh
|
||||||
|
done.Store(true)
|
||||||
|
|
||||||
subTracks := m.GetSubscribedTracks()
|
subTracks := m.GetSubscribedTracks()
|
||||||
downTracksToClose := make([]*sfu.DownTrack, 0, len(subTracks))
|
downTracksToClose := make([]*sfu.DownTrack, 0, len(subTracks))
|
||||||
@@ -127,6 +141,7 @@ func (m *SubscriptionManager) Close(isExpectedToResume bool) {
|
|||||||
go dt.CloseWithFlush(true)
|
go dt.CloseWithFlush(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
downTracksClosed.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SubscriptionManager) isClosed() bool {
|
func (m *SubscriptionManager) isClosed() bool {
|
||||||
|
|||||||
+56
-2
@@ -141,7 +141,7 @@ func (s signal) String() string {
|
|||||||
type event struct {
|
type event struct {
|
||||||
*PCTransport
|
*PCTransport
|
||||||
signal signal
|
signal signal
|
||||||
data interface{}
|
data any
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e event) String() string {
|
func (e event) String() string {
|
||||||
@@ -280,6 +280,11 @@ type PCTransport struct {
|
|||||||
signalStateCheckTimer *time.Timer
|
signalStateCheckTimer *time.Timer
|
||||||
currentOfferIceCredential string // ice user:pwd, for publish side ice restart checking
|
currentOfferIceCredential string // ice user:pwd, for publish side ice restart checking
|
||||||
pendingRestartIceOffer *webrtc.SessionDescription
|
pendingRestartIceOffer *webrtc.SessionDescription
|
||||||
|
|
||||||
|
// CLOSE-DEBUG-CLEANUP
|
||||||
|
iceGatheringState atomic.Value // webrtc.ICEGatheringState
|
||||||
|
iceConnectionState atomic.Value // webrtc.ICEConnectionState
|
||||||
|
peerConnectionState atomic.Value // webrtc.PeerConnectionState
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransportParams struct {
|
type TransportParams struct {
|
||||||
@@ -514,6 +519,9 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
|||||||
connectionDetails: types.NewICEConnectionDetails(params.Transport, params.Logger),
|
connectionDetails: types.NewICEConnectionDetails(params.Transport, params.Logger),
|
||||||
lastNegotiate: time.Now(),
|
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))
|
t.localOfferId.Store(uint32(rand.Intn(1<<8) + 1))
|
||||||
|
|
||||||
bwe, err := t.createPeerConnection()
|
bwe, err := t.createPeerConnection()
|
||||||
@@ -768,6 +776,7 @@ func (t *PCTransport) setConnectedAt(at time.Time) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) onICEGatheringStateChange(state webrtc.ICEGatheringState) {
|
func (t *PCTransport) onICEGatheringStateChange(state webrtc.ICEGatheringState) {
|
||||||
|
t.iceGatheringState.Store(state)
|
||||||
t.params.Logger.Debugw("ice gathering state change", "state", state.String())
|
t.params.Logger.Debugw("ice gathering state change", "state", state.String())
|
||||||
if state != webrtc.ICEGatheringStateComplete {
|
if state != webrtc.ICEGatheringStateComplete {
|
||||||
return
|
return
|
||||||
@@ -799,6 +808,7 @@ func (t *PCTransport) handleConnectionFailed(forceShortConn bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState) {
|
func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState) {
|
||||||
|
t.iceConnectionState.Store(state)
|
||||||
t.params.Logger.Debugw("ice connection state change", "state", state.String())
|
t.params.Logger.Debugw("ice connection state change", "state", state.String())
|
||||||
switch state {
|
switch state {
|
||||||
case webrtc.ICEConnectionStateConnected:
|
case webrtc.ICEConnectionStateConnected:
|
||||||
@@ -810,6 +820,7 @@ func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) onPeerConnectionStateChange(state webrtc.PeerConnectionState) {
|
func (t *PCTransport) onPeerConnectionStateChange(state webrtc.PeerConnectionState) {
|
||||||
|
t.peerConnectionState.Store(state)
|
||||||
t.params.Logger.Debugw("peer connection state change", "state", state.String())
|
t.params.Logger.Debugw("peer connection state change", "state", state.String())
|
||||||
switch state {
|
switch state {
|
||||||
case webrtc.PeerConnectionStateConnected:
|
case webrtc.PeerConnectionStateConnected:
|
||||||
@@ -1437,15 +1448,54 @@ func (t *PCTransport) Close() {
|
|||||||
return
|
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()
|
<-t.eventsQueue.Stop()
|
||||||
|
eventsQueueDone.Store(true)
|
||||||
t.clearSignalStateCheckTimer()
|
t.clearSignalStateCheckTimer()
|
||||||
|
|
||||||
if t.streamAllocator != nil {
|
if t.streamAllocator != nil {
|
||||||
t.streamAllocator.Stop()
|
t.streamAllocator.Stop()
|
||||||
}
|
}
|
||||||
|
streamAllocatorStopped.Store(true)
|
||||||
if t.pacer != nil {
|
if t.pacer != nil {
|
||||||
t.pacer.Stop()
|
t.pacer.Stop()
|
||||||
}
|
}
|
||||||
|
pacerStopped.Store(true)
|
||||||
|
|
||||||
t.clearConnTimer()
|
t.clearConnTimer()
|
||||||
|
|
||||||
@@ -1459,19 +1509,23 @@ func (t *PCTransport) Close() {
|
|||||||
t.reliableDC.Close()
|
t.reliableDC.Close()
|
||||||
t.reliableDC = nil
|
t.reliableDC = nil
|
||||||
}
|
}
|
||||||
|
reliableDataChannelClosed.Store(true)
|
||||||
|
|
||||||
if t.lossyDC != nil {
|
if t.lossyDC != nil {
|
||||||
t.lossyDC.Close()
|
t.lossyDC.Close()
|
||||||
t.lossyDC = nil
|
t.lossyDC = nil
|
||||||
}
|
}
|
||||||
|
lossyDataChannelClosed.Store(true)
|
||||||
|
|
||||||
for _, dc := range t.unlabeledDataChannels {
|
for _, dc := range t.unlabeledDataChannels {
|
||||||
dc.Close()
|
dc.Close()
|
||||||
}
|
}
|
||||||
t.unlabeledDataChannels = nil
|
t.unlabeledDataChannels = nil
|
||||||
|
unlabeledDataChannelClosed.Store(true)
|
||||||
t.lock.Unlock()
|
t.lock.Unlock()
|
||||||
|
|
||||||
_ = t.pc.Close()
|
_ = t.pc.Close()
|
||||||
|
peerConnectionClosed.Store(true)
|
||||||
|
|
||||||
t.outputAndClearICEStats()
|
t.outputAndClearICEStats()
|
||||||
}
|
}
|
||||||
@@ -2873,7 +2927,7 @@ func (t *PCTransport) handleICERestart(_ event) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) onNegotiationFailed(warning bool, reason string) {
|
func (t *PCTransport) onNegotiationFailed(warning bool, reason string) {
|
||||||
logFields := []interface{}{
|
logFields := []any{
|
||||||
"reason", reason,
|
"reason", reason,
|
||||||
"localCurrent", t.pc.CurrentLocalDescription(),
|
"localCurrent", t.pc.CurrentLocalDescription(),
|
||||||
"localPending", t.pc.PendingLocalDescription(),
|
"localPending", t.pc.PendingLocalDescription(),
|
||||||
|
|||||||
@@ -199,16 +199,40 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) Close() {
|
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 {
|
if t.publisher != nil {
|
||||||
t.publisher.Close()
|
t.publisher.Close()
|
||||||
}
|
}
|
||||||
|
publisherClosed.Store(true)
|
||||||
if t.subscriber != nil {
|
if t.subscriber != nil {
|
||||||
t.subscriber.Close()
|
t.subscriber.Close()
|
||||||
}
|
}
|
||||||
|
subscriberClosed.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) SubscriberClose() {
|
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()
|
t.subscriber.Close()
|
||||||
|
subscriberClosed.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) HasPublisherEverConnected() bool {
|
func (t *TransportManager) HasPublisherEverConnected() bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user