fix(desktop): time out ICE gathering

This commit is contained in:
Haitao Pan 2026-06-13 07:10:28 +08:00
parent 01c2c2ed31
commit a19456d8a8
2 changed files with 39 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import (
const (
desktopReliableInputChannelLabel = "input"
desktopMoveInputChannelLabel = "input-move"
desktopICEGatheringTimeout = 10 * time.Second
)
type WebRTCServer struct {
@ -230,7 +231,9 @@ func (w *WebRTCServer) ProcessOffer(sdpOffer string) (string, error) {
return "", fmt.Errorf("failed to set local description: %w", err)
}
<-gatherComplete
if err := waitForICEGatheringComplete(gatherComplete, desktopICEGatheringTimeout); err != nil {
return "", err
}
localDesc := pc.LocalDescription()
if localDesc == nil {
@ -240,6 +243,15 @@ func (w *WebRTCServer) ProcessOffer(sdpOffer string) (string, error) {
return localDesc.SDP, nil
}
func waitForICEGatheringComplete(done <-chan struct{}, timeout time.Duration) error {
select {
case <-done:
return nil
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for ICE gathering after %s", timeout)
}
}
// AddICECandidate adds a remote ICE candidate
func (w *WebRTCServer) AddICECandidate(candidate webrtc.ICECandidateInit) error {
w.mu.Lock()

View File

@ -1,6 +1,9 @@
package desktop
import "testing"
import (
"testing"
"time"
)
func TestIsDesktopInputDataChannelLabelAllowsReliableAndMoveChannels(t *testing.T) {
if !isDesktopInputDataChannelLabel(desktopReliableInputChannelLabel) {
@ -13,3 +16,25 @@ func TestIsDesktopInputDataChannelLabelAllowsReliableAndMoveChannels(t *testing.
t.Fatalf("expected unrelated data channel label to be ignored")
}
}
func TestWaitForICEGatheringCompleteReturnsWhenDoneCloses(t *testing.T) {
done := make(chan struct{})
close(done)
if err := waitForICEGatheringComplete(done, time.Second); err != nil {
t.Fatalf("expected closed gathering channel to succeed: %v", err)
}
}
func TestWaitForICEGatheringCompleteTimesOut(t *testing.T) {
done := make(chan struct{})
start := time.Now()
err := waitForICEGatheringComplete(done, 10*time.Millisecond)
if err == nil {
t.Fatalf("expected timeout error")
}
if elapsed := time.Since(start); elapsed > time.Second {
t.Fatalf("timeout helper waited too long: %s", elapsed)
}
}