Skip to content

Commit 7eccb81

Browse files
committed
Lock remote session description being applied
1 parent 700afe4 commit 7eccb81

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

.github/workflows/coder.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ jobs:
149149
- run: go install gotest.tools/gotestsum@latest
150150

151151
- uses: hashicorp/setup-terraform@v1
152+
if: runner.os == 'Linux'
152153
with:
153154
terraform_version: 1.1.2
155+
terraform_wrapper: false
154156

155157
- name: Test with Mock Database
156158
run:

peer/conn.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
7979
// This channel needs to be bufferred otherwise slow consumers
8080
// of this will cause a connection failure.
8181
localNegotiator: make(chan Negotiation, 8),
82-
remoteSessionDescription: make(chan webrtc.SessionDescription, 1),
82+
remoteSessionDescription: make(chan webrtc.SessionDescription),
8383
pendingCandidatesToSend: make([]webrtc.ICECandidateInit, 0),
8484
}
8585
if client {
@@ -124,8 +124,9 @@ type Conn struct {
124124
dcFailedListeners atomic.Uint32
125125
dcClosedWaitGroup sync.WaitGroup
126126

127-
localNegotiator chan Negotiation
128-
remoteSessionDescription chan webrtc.SessionDescription
127+
localNegotiator chan Negotiation
128+
remoteSessionDescription chan webrtc.SessionDescription
129+
remoteSessionDescriptionMutex sync.Mutex
129130

130131
pendingCandidatesToSend []webrtc.ICECandidateInit
131132
pendingCandidatesToSendMutex sync.Mutex
@@ -329,9 +330,6 @@ func (c *Conn) negotiate() {
329330
return
330331
}
331332
c.opts.Logger.Debug(context.Background(), "setting local description", slog.F("closed", c.isClosed()))
332-
if c.isClosed() {
333-
return
334-
}
335333
c.closeMutex.Lock()
336334
err = c.rtc.SetLocalDescription(offer)
337335
c.closeMutex.Unlock()
@@ -353,6 +351,9 @@ func (c *Conn) negotiate() {
353351
case sessionDescription = <-c.remoteSessionDescription:
354352
}
355353

354+
// This prevents candidates from being added while
355+
// the remote description is being set.
356+
c.remoteSessionDescriptionMutex.Lock()
356357
c.opts.Logger.Debug(context.Background(), "setting remote description")
357358
c.closeMutex.Lock()
358359
err := c.rtc.SetRemoteDescription(sessionDescription)
@@ -361,17 +362,17 @@ func (c *Conn) negotiate() {
361362
_ = c.CloseWithError(xerrors.Errorf("set remote description (closed %v): %w", c.isClosed(), err))
362363
return
363364
}
365+
c.remoteSessionDescriptionMutex.Unlock()
364366

365367
if !c.offerrer {
368+
c.closeMutex.Lock()
366369
answer, err := c.rtc.CreateAnswer(&webrtc.AnswerOptions{})
370+
c.closeMutex.Unlock()
367371
if err != nil {
368372
_ = c.CloseWithError(xerrors.Errorf("create answer: %w", err))
369373
return
370374
}
371375
c.opts.Logger.Debug(context.Background(), "setting local description", slog.F("closed", c.isClosed()))
372-
if c.isClosed() {
373-
return
374-
}
375376
c.closeMutex.Lock()
376377
err = c.rtc.SetLocalDescription(answer)
377378
c.closeMutex.Unlock()
@@ -418,10 +419,10 @@ func (c *Conn) AddRemoteNegotiation(negotiation Negotiation) error {
418419
}
419420

420421
if len(negotiation.ICECandidates) > 0 {
422+
c.remoteSessionDescriptionMutex.Lock()
423+
defer c.remoteSessionDescriptionMutex.Unlock()
421424
c.opts.Logger.Debug(context.Background(), "adding remote negotiation with ice candidates",
422425
slog.F("count", len(negotiation.ICECandidates)))
423-
c.closeMutex.Lock()
424-
defer c.closeMutex.Unlock()
425426
for _, iceCandidate := range negotiation.ICECandidates {
426427
err := c.rtc.AddICECandidate(iceCandidate)
427428
if err != nil {

0 commit comments

Comments
 (0)