Skip to content
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

Fix data race of RTX packet #2595

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Aaron Boushley <boushley@pretzelaux.com>
Aaron France <aaron.l.france@gmail.com>
Adam Kiss <masterada@gmail.com>
Aditya Kumar <k.aditya00@gmail.com>
Adrian Cable <6544927+adriancable@users.noreply.github.com>
Adrian Cable <adrian.cable@gmail.com>
adwpc <adwpc@hotmail.com>
aggresss <aggresss@163.com>
Expand Down Expand Up @@ -200,6 +201,7 @@ Steffen Vogel <post@steffenvogel.de>
stephanrotolante <stephanrotolante@gmail.com>
streamer45 <cstcld91@gmail.com>
Suhas Gaddam <suhas.g.2011@gmail.com>
sukun <sukunrt@gmail.com>
Suzuki Takeo <takeo@stko.info>
sylba2050 <masataka.hisasue@optim.co.jp>
Tarrence van As <tarrencev@users.noreply.github.com>
Expand Down
21 changes: 19 additions & 2 deletions rtpreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
type rtxPacketWithAttributes struct {
pkt []byte
attributes interceptor.Attributes
pool *sync.Pool
}

func (p *rtxPacketWithAttributes) release() {
if p.pkt != nil {
b := p.pkt[:cap(p.pkt)]
p.pool.Put(b) // nolint:staticcheck
p.pkt = nil
}

Check warning on line 54 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L49-L54

Added lines #L49 - L54 were not covered by tests
}

// RTPReceiver allows an application to inspect the receipt of a TrackRemote
Expand All @@ -59,6 +68,8 @@

// A reference to the associated api object
api *API

rtxPool sync.Pool
}

// NewRTPReceiver constructs a new RTPReceiver
Expand All @@ -74,6 +85,9 @@
closed: make(chan interface{}),
received: make(chan interface{}),
tracks: []trackStreams{},
rtxPool: sync.Pool{New: func() interface{} {
return make([]byte, api.settingEngine.getReceiveMTU())
}},

Check warning on line 90 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L89-L90

Added lines #L89 - L90 were not covered by tests
}

return r, nil
Expand Down Expand Up @@ -420,10 +434,11 @@
track.repairStreamChannel = make(chan rtxPacketWithAttributes)

go func() {
b := make([]byte, r.api.settingEngine.getReceiveMTU())
for {
b := r.rtxPool.Get().([]byte) // nolint:forcetypeassert

Check warning on line 438 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L438

Added line #L438 was not covered by tests
i, attributes, err := track.repairInterceptor.Read(b, nil)
if err != nil {
r.rtxPool.Put(b) // nolint:staticcheck

Check warning on line 441 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L441

Added line #L441 was not covered by tests
return
}

Expand All @@ -444,6 +459,7 @@

if i-int(headerLength)-paddingLength < 2 {
// BWE probe packet, ignore
r.rtxPool.Put(b) // nolint:staticcheck

Check warning on line 462 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L462

Added line #L462 was not covered by tests
continue
}

Expand All @@ -459,8 +475,9 @@

select {
case <-r.closed:
r.rtxPool.Put(b) // nolint:staticcheck

Check warning on line 478 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L478

Added line #L478 was not covered by tests
return
case track.repairStreamChannel <- rtxPacketWithAttributes{pkt: b[:i-2], attributes: attributes}:
case track.repairStreamChannel <- rtxPacketWithAttributes{pkt: b[:i-2], attributes: attributes, pool: &r.rtxPool}:

Check warning on line 480 in rtpreceiver.go

View check run for this annotation

Codecov / codecov/patch

rtpreceiver.go#L480

Added line #L480 was not covered by tests
}
}
}()
Expand Down
1 change: 1 addition & 0 deletions track_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
if rtxPacketReceived := r.readRTX(t); rtxPacketReceived != nil {
n = copy(b, rtxPacketReceived.pkt)
attributes = rtxPacketReceived.attributes
rtxPacketReceived.release()

Check warning on line 134 in track_remote.go

View check run for this annotation

Codecov / codecov/patch

track_remote.go#L134

Added line #L134 was not covered by tests
err = nil
} else {
// If there's no separate RTX track (or there's a separate RTX track but no RTX packet waiting), wait for and return
Expand Down
Loading