Skip to content

Commit

Permalink
fix: lock EBML write and close ops
Browse files Browse the repository at this point in the history
Avoids a race condition that allowed writing to closed diskwriters (panic)
  • Loading branch information
prlanzarin committed Dec 7, 2023
1 parent ee2b1b7 commit fb4219e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion internal/webrtc/recorder/webm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
log "github.com/sirupsen/logrus"
"os"
"sync"
"time"
)

Expand All @@ -24,6 +25,7 @@ type WebmRecorder struct {
ctx context.Context
file string
fileMode os.FileMode
m sync.Mutex

audioWriter, videoWriter webm.BlockWriteCloser
audioBuilder, videoBuilder *samplebuilder.SampleBuilder
Expand Down Expand Up @@ -81,7 +83,8 @@ func (r *WebmRecorder) PushAudio(p *rtp.Packet) {
r.pushOpus(p)
}

func (r *WebmRecorder) Close() time.Duration {
// Locked
func (r *WebmRecorder) close() time.Duration {
if r.closed {
return r.videoTimestamp
}
Expand All @@ -104,10 +107,22 @@ func (r *WebmRecorder) Close() time.Duration {
log.WithField("session", r.ctx.Value("session")).
Print("webm writer closed without starting")
}

return r.videoTimestamp
}

func (r *WebmRecorder) Close() time.Duration {
r.m.Lock()
ts := r.close()
r.m.Unlock()

return ts
}

func (r *WebmRecorder) pushOpus(p *rtp.Packet) {
r.m.Lock()
defer r.m.Unlock()

r.audioBuilder.Push(p)

for {
Expand All @@ -125,6 +140,9 @@ func (r *WebmRecorder) pushOpus(p *rtp.Packet) {
}

func (r *WebmRecorder) pushVP8(p *rtp.Packet) {
r.m.Lock()
defer r.m.Unlock()

if len(p.Payload) == 0 || r.closed {
return
}
Expand Down

0 comments on commit fb4219e

Please sign in to comment.