File tree 3 files changed +18
-5
lines changed
3 files changed +18
-5
lines changed Original file line number Diff line number Diff line change 6
6
"io"
7
7
"net"
8
8
"sync"
9
+ "time"
9
10
10
11
"github.com/pion/datachannel"
11
12
"github.com/pion/webrtc/v3"
@@ -244,6 +245,14 @@ func (c *Channel) Write(bytes []byte) (n int, err error) {
244
245
if c .dc .BufferedAmount ()+ uint64 (len (bytes )) >= maxBufferedAmount {
245
246
<- c .sendMore
246
247
}
248
+
249
+ // There's an obvious race-condition here. This is an edge-case, as
250
+ // most-frequently data won't be pooled so synchronously, but is
251
+ // definitely possible.
252
+ //
253
+ // See: https://github.com/pion/sctp/issues/181
254
+ time .Sleep (time .Microsecond )
255
+
247
256
return c .rwc .Write (bytes )
248
257
}
249
258
Original file line number Diff line number Diff line change @@ -125,7 +125,7 @@ type Conn struct {
125
125
remoteSessionDescriptionChannel chan webrtc.SessionDescription
126
126
127
127
negotiateMutex sync.Mutex
128
- hasNegotiated bool
128
+ hasNegotiated atomic. Bool
129
129
130
130
loggerValue atomic.Value
131
131
settingEngine webrtc.SettingEngine
@@ -290,10 +290,9 @@ func (c *Conn) negotiate() {
290
290
c .logger ().Debug (context .Background (), "negotiating" )
291
291
// ICE candidates cannot be added until SessionDescriptions have been
292
292
// exchanged between peers.
293
- if c .hasNegotiated {
293
+ if c .hasNegotiated . Swap ( true ) {
294
294
c .negotiateMutex .Lock ()
295
295
}
296
- c .hasNegotiated = true
297
296
defer c .negotiateMutex .Unlock ()
298
297
299
298
if c .offerer {
@@ -605,6 +604,13 @@ func (c *Conn) CloseWithError(err error) error {
605
604
// All logging, goroutines, and async functionality is cleaned up after this.
606
605
c .dcClosedWaitGroup .Wait ()
607
606
607
+ // It's possible the connection can be closed before negotiation has
608
+ // began. In this case, we want to unlock the mutex to unblock any
609
+ // pending candidates being flushed.
610
+ if ! c .hasNegotiated .Load () {
611
+ c .negotiateMutex .Unlock ()
612
+ }
613
+
608
614
// Disable logging!
609
615
c .loggerValue .Store (slog.Logger {})
610
616
logger .Sync ()
Original file line number Diff line number Diff line change @@ -59,9 +59,7 @@ func TestMain(m *testing.M) {
59
59
}
60
60
61
61
func TestConn (t * testing.T ) {
62
- t .Skip ("known flake -- https://github.com/coder/coder/issues/1644" )
63
62
t .Parallel ()
64
-
65
63
t .Run ("Ping" , func (t * testing.T ) {
66
64
t .Parallel ()
67
65
client , server , _ := createPair (t )
You can’t perform that action at this time.
0 commit comments