Skip to content

Commit

Permalink
APP-6839: Call buffer low broadcast with cond variable mutex held. (#387
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dgottlieb authored Nov 12, 2024
1 parent e8de541 commit 000ac9e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rpc/wrtc_base_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,19 @@ func (ch *webrtcBaseChannel) closeWithReason(err error) error {
if ch.closed {
return nil
}

// APP-6839: We must hold the `bufferWriteMu` to avoid a "missed notification" that can happen
// when a `webrtcBaseChannel.write` happens concurrently with `closeWithReason`. Specifically,
// this lock makes atomic the `ch.cancel` with the broadcast. Such that a call to write that can
// `Wait` on this condition variable must either:
// - Observe the context being canceled, or
// - Call `Wait` before* the following `Broadcast` is invoked.
ch.bufferWriteMu.Lock()
ch.closed = true
ch.closedReason = err
ch.cancel()
ch.bufferWriteCond.Broadcast()
ch.bufferWriteMu.Unlock()

// Underlying connection may already be closed; ignore "conn is closed"
// errors.
Expand Down Expand Up @@ -232,6 +241,7 @@ func (ch *webrtcBaseChannel) write(msg proto.Message) error {
if ch.ctx.Err() != nil {
return io.ErrClosedPipe
}

// RSDK-9239: Only wait when we're strictly over the threshold. Pion invokes the registered
// callback (notify `bufferWriteCond`) when moving from larger than bufferThreshold to less
// than or equal to.
Expand Down

0 comments on commit 000ac9e

Please sign in to comment.