fix(input): retire the channel after a rejected write
This commit is contained in:
1 parent
1cc8ebb062
commit
998e9f5039
2 files changed
+75
-3
No files matched your search
@@ -66,7 +66,9 @@ func send(channel *webrtc.DataChannel, p Packet) {
|
||||
return
|
||||
}
|
||||
data, _ := json.Marshal(p)
|
||||
_ = channel.SendText(string(data))
|
||||
if err := channel.SendText(string(data)); err != nil {
|
||||
go channel.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Relay) AttachUp(channel *webrtc.DataChannel) {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package inputroute
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/sctp"
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
@@ -185,6 +188,65 @@ func TestResetDownInvalidatesReadBindingAcrossSameEpoch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInputReplyWriteFailureRetiresOnlyFailedLeg(t *testing.T) {
|
||||
root := New(func(string, Packet, func() bool) {})
|
||||
defer root.Close()
|
||||
relay := New(nil)
|
||||
defer relay.Close()
|
||||
upstream, rootLeg := inputChannelPairWithLocalMessageLimit(t, 256)
|
||||
relay.AttachUp(upstream)
|
||||
root.AttachDown("relay", rootLeg)
|
||||
|
||||
leaf, childLeg := inputChannelPair(t)
|
||||
leafMessages := make(chan Packet, 8)
|
||||
leaf.OnMessage(func(message webrtc.DataChannelMessage) {
|
||||
if packet, ok := decode(message); ok {
|
||||
leafMessages <- packet
|
||||
}
|
||||
})
|
||||
relay.AttachDown("leaf", childLeg)
|
||||
for {
|
||||
packet := receiveInputPacket(t, leafMessages)
|
||||
if packet.Ready != nil && *packet.Ready {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
healthy, healthyLeg := inputChannelPair(t)
|
||||
replies := make(chan Packet, 8)
|
||||
healthy.OnMessage(func(message webrtc.DataChannelMessage) {
|
||||
if packet, ok := decode(message); ok && packet.Ready == nil {
|
||||
replies <- packet
|
||||
}
|
||||
})
|
||||
root.AttachDown("healthy", healthyLeg)
|
||||
|
||||
// A real Pion write rejects this small relay packet against the injected
|
||||
// receive limit advertised by upstream. Production uses a larger limit.
|
||||
reply := Packet{ID: strings.Repeat("a", 32), Epoch: strings.Repeat("b", 32), Data: strings.Repeat("x", 256)}
|
||||
data, err := json.Marshal(reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := rootLeg.SendText(string(data)); !errors.Is(err, sctp.ErrOutboundPacketTooLarge) {
|
||||
t.Fatalf("expected Pion to reject the reply before enqueue, got %v", err)
|
||||
}
|
||||
if rootLeg.ReadyState() != webrtc.DataChannelStateOpen || rootLeg.BufferedAmount() != 0 {
|
||||
t.Fatal("injected write failure closed the input leg or enqueued the rejected reply")
|
||||
}
|
||||
root.Reply(reply)
|
||||
if got := receiveInputPacket(t, replies); got != reply {
|
||||
t.Fatalf("healthy input leg got %+v", got)
|
||||
}
|
||||
packet := receiveInputPacket(t, leafMessages)
|
||||
if packet.Ready == nil || *packet.Ready {
|
||||
t.Fatalf("leaf did not lose readiness: %+v", packet)
|
||||
}
|
||||
if rootLeg.Transport().State() != webrtc.SCTPTransportStateConnected || leaf.ReadyState() != webrtc.DataChannelStateOpen {
|
||||
t.Fatal("input write failure retired the shared transport or the leaf's own channel")
|
||||
}
|
||||
}
|
||||
|
||||
func receiveInputPacket(t *testing.T, packets <-chan Packet) Packet {
|
||||
t.Helper()
|
||||
select {
|
||||
@@ -209,17 +271,25 @@ func waitInputChannel(t *testing.T, label string, ready func() bool) {
|
||||
}
|
||||
|
||||
func inputChannelPair(t *testing.T) (*webrtc.DataChannel, *webrtc.DataChannel) {
|
||||
t.Helper()
|
||||
return inputChannelPairWithLocalMessageLimit(t, 0)
|
||||
}
|
||||
|
||||
func inputChannelPairWithLocalMessageLimit(t *testing.T, maxMessageSize uint32) (*webrtc.DataChannel, *webrtc.DataChannel) {
|
||||
t.Helper()
|
||||
settings := webrtc.SettingEngine{}
|
||||
settings.SetIncludeLoopbackCandidate(true)
|
||||
settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
|
||||
api := webrtc.NewAPI(webrtc.WithSettingEngine(settings))
|
||||
localSettings := settings
|
||||
localSettings.SetSCTPMaxMessageSize(maxMessageSize)
|
||||
api := webrtc.NewAPI(webrtc.WithSettingEngine(localSettings))
|
||||
a, err := api.NewPeerConnection(webrtc.Configuration{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = a.Close() })
|
||||
b, err := api.NewPeerConnection(webrtc.Configuration{})
|
||||
receiverAPI := webrtc.NewAPI(webrtc.WithSettingEngine(settings))
|
||||
b, err := receiverAPI.NewPeerConnection(webrtc.Configuration{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user