Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Last active November 5, 2021 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sean-Der/5f37e64bacd6a7dd6ba9df5faeff3f2e to your computer and use it in GitHub Desktop.
Save Sean-Der/5f37e64bacd6a7dd6ba9df5faeff3f2e to your computer and use it in GitHub Desktop.
DataChannel race
diff --git a/peerconnection.go b/peerconnection.go
index a5d50ee..5dbf46e 100644
--- a/peerconnection.go
+++ b/peerconnection.go
@@ -1313,28 +1313,6 @@ func (pc *PeerConnection) startSCTP() {
return
}
-
- // DataChannels that need to be opened now that SCTP is available
- // make a copy we may have incoming DataChannels mutating this while we open
- pc.sctpTransport.lock.RLock()
- dataChannels := append([]*DataChannel{}, pc.sctpTransport.dataChannels...)
- pc.sctpTransport.lock.RUnlock()
-
- var openedDCCount uint32
- for _, d := range dataChannels {
- if d.ReadyState() == DataChannelStateConnecting {
- err := d.open(pc.sctpTransport)
- if err != nil {
- pc.log.Warnf("failed to open data channel: %s", err)
- continue
- }
- openedDCCount++
- }
- }
-
- pc.sctpTransport.lock.Lock()
- pc.sctpTransport.dataChannelsOpened += openedDCCount
- pc.sctpTransport.lock.Unlock()
}
func (pc *PeerConnection) handleUndeclaredSSRC(ssrc SSRC, remoteDescription *SessionDescription) (handled bool, err error) {
diff --git a/sctptransport.go b/sctptransport.go
index 41c635b..ef4b746 100644
--- a/sctptransport.go
+++ b/sctptransport.go
@@ -115,6 +115,28 @@ func (r *SCTPTransport) Start(remoteCaps SCTPCapabilities) error {
r.sctpAssociation = sctpAssociation
r.state = SCTPTransportStateConnected
+ // DataChannels that need to be opened now that SCTP is available
+ // make a copy we may have incoming DataChannels mutating this while we open
+ r.lock.RLock()
+ dataChannels := append([]*DataChannel{}, r.dataChannels...)
+ r.lock.RUnlock()
+
+ var openedDCCount uint32
+ for _, d := range dataChannels {
+ if d.ReadyState() == DataChannelStateConnecting {
+ err := d.open(r)
+ if err != nil {
+ r.log.Warnf("failed to open data channel: %s", err)
+ continue
+ }
+ openedDCCount++
+ }
+ }
+
+ r.lock.Lock()
+ r.dataChannelsOpened += openedDCCount
+ r.lock.Unlock()
+
go r.acceptDataChannels(sctpAssociation)
return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment