Skip to content

fix: Close peer negotiate mutex if we haven't negotiated #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Close peer negotiate mutex if we haven't negotiated
Closes #1706 and #1644.
  • Loading branch information
kylecarbs committed May 27, 2022
commit a7a1ec5fb6879240771aa9b0557dae45317deef3
9 changes: 9 additions & 0 deletions peer/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"sync"
"time"

"github.com/pion/datachannel"
"github.com/pion/webrtc/v3"
Expand Down Expand Up @@ -244,6 +245,14 @@ func (c *Channel) Write(bytes []byte) (n int, err error) {
if c.dc.BufferedAmount()+uint64(len(bytes)) >= maxBufferedAmount {
<-c.sendMore
}

// There's an obvious race-condition here. This is an edge-case, as
// most-frequently data won't be pooled so synchronously, but is
// definitely possible.
//
// See: https://github.com/pion/sctp/issues/181
time.Sleep(time.Microsecond)

return c.rwc.Write(bytes)
}

Expand Down
34 changes: 22 additions & 12 deletions peer/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
dcFailedChannel: make(chan struct{}),
localCandidateChannel: make(chan webrtc.ICECandidateInit),
localSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
negotiated: make(chan struct{}),
remoteSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
settingEngine: opts.SettingEngine,
}
Expand Down Expand Up @@ -124,8 +125,7 @@ type Conn struct {
localSessionDescriptionChannel chan webrtc.SessionDescription
remoteSessionDescriptionChannel chan webrtc.SessionDescription

negotiateMutex sync.Mutex
hasNegotiated bool
negotiated chan struct{}

loggerValue atomic.Value
settingEngine webrtc.SettingEngine
Expand All @@ -152,9 +152,6 @@ func (c *Conn) logger() slog.Logger {
}

func (c *Conn) init() error {
// The negotiation needed callback can take a little bit to execute!
c.negotiateMutex.Lock()

c.rtc.OnNegotiationNeeded(c.negotiate)
c.rtc.OnICEConnectionStateChange(func(iceConnectionState webrtc.ICEConnectionState) {
c.closedICEMutex.Lock()
Expand Down Expand Up @@ -290,11 +287,13 @@ func (c *Conn) negotiate() {
c.logger().Debug(context.Background(), "negotiating")
// ICE candidates cannot be added until SessionDescriptions have been
// exchanged between peers.
if c.hasNegotiated {
c.negotiateMutex.Lock()
}
c.hasNegotiated = true
defer c.negotiateMutex.Unlock()
defer func() {
select {
case <-c.negotiated:
default:
close(c.negotiated)
}
}()

if c.offerer {
offer, err := c.rtc.CreateOffer(&webrtc.OfferOptions{})
Expand Down Expand Up @@ -368,8 +367,10 @@ func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) {
// This must occur in a goroutine to allow the SessionDescriptions
// to be exchanged first.
go func() {
c.negotiateMutex.Lock()
defer c.negotiateMutex.Unlock()
select {
case <-c.closed:
case <-c.negotiated:
}
if c.isClosed() {
return
}
Expand Down Expand Up @@ -605,6 +606,15 @@ func (c *Conn) CloseWithError(err error) error {
// All logging, goroutines, and async functionality is cleaned up after this.
c.dcClosedWaitGroup.Wait()

// It's possible the connection can be closed before negotiation has
// began. In this case, we want to unlock the mutex to unblock any
// pending candidates being flushed.
select {
case <-c.negotiated:
default:
close(c.negotiated)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you close(c.closed) sooner?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, but you did bring up a good point about the second close not being needed. Fixed!


// Disable logging!
c.loggerValue.Store(slog.Logger{})
logger.Sync()
Expand Down
2 changes: 0 additions & 2 deletions peer/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func TestMain(m *testing.M) {
}

func TestConn(t *testing.T) {
t.Skip("known flake -- https://github.com/coder/coder/issues/1644")
t.Parallel()

t.Run("Ping", func(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
Expand Down