From 6509bb0325d8208e5305dcf29547398469f919d9 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Fri, 1 Sep 2023 17:59:25 +0530 Subject: [PATCH] Add option to issue full reconnect on data channel error. (#2026) * Add option to issue full reconnect on data channel error. There are situations where send data packet fails because of "stream closed". It is unclear when that happens. Seems to be after an ICERestart after ICE failed and connection type switching to TURN from ICE. Once the failure happens, it is not recoverable. Potentially, it is recoverable, but unclear where the problem lies. Attempts to reproduce looking at the pattern of failures has been unsuccesful. In the mean time, adding an option to issue full reconnect when send data packet fails. * typo --- pkg/config/config.go | 3 +++ pkg/rtc/errors.go | 1 + pkg/rtc/participant.go | 12 +++++++++++- pkg/rtc/transport.go | 10 ++++++---- pkg/rtc/types/interfaces.go | 8 +++++++- pkg/service/roommanager.go | 6 ++++++ 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 36828229..828ba0bf 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -102,6 +102,9 @@ type RTCConfig struct { // force a reconnect on a subscription error ReconnectOnSubscriptionError *bool `yaml:"reconnect_on_subscription_error,omitempty"` + + // force a reconnect on a data channel error + ReconnectOnDataChannelError *bool `yaml:"reconnect_on_data_channel_error,omitempty"` } type TURNServer struct { diff --git a/pkg/rtc/errors.go b/pkg/rtc/errors.go index 383afde0..65699563 100644 --- a/pkg/rtc/errors.go +++ b/pkg/rtc/errors.go @@ -23,6 +23,7 @@ var ( ErrLimitExceeded = errors.New("node has exceeded its configured limit") ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room") ErrDataChannelUnavailable = errors.New("data channel is not available") + ErrTransportFailure = errors.New("transport failure") ErrEmptyIdentity = errors.New("participant identity cannot be empty") ErrEmptyParticipantID = errors.New("participant ID cannot be empty") ErrMissingGrants = errors.New("VideoGrant is missing") diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 3b4518c5..005ff514 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -16,6 +16,7 @@ package rtc import ( "context" + "io" "os" "strconv" "strings" @@ -24,6 +25,7 @@ import ( lru "github.com/hashicorp/golang-lru/v2" "github.com/pion/rtcp" + "github.com/pion/sctp" "github.com/pion/sdp/v3" "github.com/pion/webrtc/v3" "github.com/pkg/errors" @@ -103,6 +105,7 @@ type ParticipantParams struct { GetParticipantInfo func(pID livekit.ParticipantID) *livekit.ParticipantInfo ReconnectOnPublicationError bool ReconnectOnSubscriptionError bool + ReconnectOnDataChannelError bool VersionGenerator utils.TimedVersionGenerator TrackResolver types.MediaTrackResolver DisableDynacast bool @@ -2143,6 +2146,8 @@ func (p *ParticipantImpl) IssueFullReconnect(reason types.ParticipantCloseReason scr = types.SignallingCloseReasonFullReconnectPublicationError case types.ParticipantCloseReasonSubscriptionError: scr = types.SignallingCloseReasonFullReconnectSubscriptionError + case types.ParticipantCloseReasonDataChannelError: + scr = types.SignallingCloseReasonFullReconnectDataChannelError case types.ParticipantCloseReasonNegotiateFailed: scr = types.SignallingCloseReasonFullReconnectNegotiateFailed } @@ -2251,7 +2256,12 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket, data []byte) er } err := p.TransportManager.SendDataPacket(dp, data) - if err == nil { + if err != nil { + if (err == sctp.ErrStreamClosed || err == io.ErrClosedPipe) && p.params.ReconnectOnDataChannelError { + p.params.Logger.Infow("issuing full reconnect on data channel error") + p.IssueFullReconnect(types.ParticipantCloseReasonDataChannelError) + } + } else { p.dataChannelStats.AddBytes(uint64(len(data)), true) } return err diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index fc13fe31..47464e6b 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -662,9 +662,9 @@ func (t *PCTransport) onPeerConnectionStateChange(state webrtc.PeerConnectionSta } func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) { + t.params.Logger.Debugw(dc.Label() + " data channel open") switch dc.Label() { case ReliableDataChannel: - t.params.Logger.Debugw("reliable data channel open") t.lock.Lock() t.reliableDC = dc t.reliableDCOpened = true @@ -677,7 +677,6 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) { t.maybeNotifyFullyEstablished() case LossyDataChannel: - t.params.Logger.Debugw("lossy data channel open") t.lock.Lock() t.lossyDC = dc t.lossyDCOpened = true @@ -762,7 +761,6 @@ func (t *PCTransport) AddTrack(trackLocal webrtc.TrackLocal, params types.AddTra } configureAudioTransceiver(transceiver, params.Stereo, !params.Red || !t.params.ClientInfo.SupportsAudioRED()) - return } @@ -833,7 +831,7 @@ func (t *PCTransport) CreateDataChannel(label string, dci *webrtc.DataChannelIni } dcErrorHandler := func(err error) { - t.params.Logger.Errorw(dc.Label()+" data channel close", err) + t.params.Logger.Errorw(dc.Label()+" data channel error", err) } t.lock.Lock() @@ -924,6 +922,10 @@ func (t *PCTransport) SendDataPacket(dp *livekit.DataPacket, data []byte) error return ErrDataChannelUnavailable } + if t.pc.ConnectionState() == webrtc.PeerConnectionStateFailed { + return ErrTransportFailure + } + return dc.Send(data) } diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index 5275b5ad..e5e44d99 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -102,6 +102,7 @@ const ( ParticipantCloseReasonOvercommitted ParticipantCloseReasonPublicationError ParticipantCloseReasonSubscriptionError + ParticipantCloseReasonDataChannelError ) func (p ParticipantCloseReason) String() string { @@ -148,6 +149,8 @@ func (p ParticipantCloseReason) String() string { return "PUBLICATION_ERROR" case ParticipantCloseReasonSubscriptionError: return "SUBSCRIPTION_ERROR" + case ParticipantCloseReasonDataChannelError: + return "DATA_CHANNEL_ERROR" default: return fmt.Sprintf("%d", int(p)) } @@ -178,7 +181,7 @@ func (p ParticipantCloseReason) ToDisconnectReason() livekit.DisconnectReason { return livekit.DisconnectReason_SERVER_SHUTDOWN case ParticipantCloseReasonOvercommitted: return livekit.DisconnectReason_SERVER_SHUTDOWN - case ParticipantCloseReasonNegotiateFailed, ParticipantCloseReasonPublicationError, ParticipantCloseReasonSubscriptionError: + case ParticipantCloseReasonNegotiateFailed, ParticipantCloseReasonPublicationError, ParticipantCloseReasonSubscriptionError, ParticipantCloseReasonDataChannelError: return livekit.DisconnectReason_STATE_MISMATCH default: // the other types will map to unknown reason @@ -197,6 +200,7 @@ const ( SignallingCloseReasonTransportFailure SignallingCloseReasonFullReconnectPublicationError SignallingCloseReasonFullReconnectSubscriptionError + SignallingCloseReasonFullReconnectDataChannelError SignallingCloseReasonFullReconnectNegotiateFailed SignallingCloseReasonParticipantClose ) @@ -215,6 +219,8 @@ func (s SignallingCloseReason) String() string { return "FULL_RECONNECT_PUBLICATION_ERROR" case SignallingCloseReasonFullReconnectSubscriptionError: return "FULL_RECONNECT_SUBSCRIPTION_ERROR" + case SignallingCloseReasonFullReconnectDataChannelError: + return "FULL_RECONNECT_DATA_CHANNEL_ERROR" case SignallingCloseReasonFullReconnectNegotiateFailed: return "FULL_RECONNECT_NEGOTIATE_FAILED" case SignallingCloseReasonParticipantClose: diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 2dec2006..6393e55b 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -355,6 +355,11 @@ func (r *RoomManager) StartSession( if r.config.RTC.ReconnectOnSubscriptionError != nil { reconnectOnSubscriptionError = *r.config.RTC.ReconnectOnSubscriptionError } + // default do not force full reconnect on a data channel error + reconnectOnDataChannelError := false + if r.config.RTC.ReconnectOnDataChannelError != nil { + reconnectOnDataChannelError = *r.config.RTC.ReconnectOnDataChannelError + } subscriberAllowPause := r.config.RTC.CongestionControl.AllowPause if pi.SubscriberAllowPause != nil { subscriberAllowPause = *pi.SubscriberAllowPause @@ -389,6 +394,7 @@ func (r *RoomManager) StartSession( }, ReconnectOnPublicationError: reconnectOnPublicationError, ReconnectOnSubscriptionError: reconnectOnSubscriptionError, + ReconnectOnDataChannelError: reconnectOnDataChannelError, VersionGenerator: r.versionGenerator, TrackResolver: room.ResolveMediaTrackForSubscriber, SubscriberAllowPause: subscriberAllowPause,