diff --git a/probetest/probetest.go b/probetest/probetest.go index 3c7c70f..6ef31fe 100644 --- a/probetest/probetest.go +++ b/probetest/probetest.go @@ -46,7 +46,7 @@ func (h ProbeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Create a PeerConnection from an SDP offer. Blocks until the gathering of ICE // candidates is complete and the answer is available in LocalDescription. func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription, - dataChan chan struct{}) (*webrtc.PeerConnection, error) { + dataChan chan struct{}, iceGatheringTimeout time.Duration) (*webrtc.PeerConnection, error) { settingsEngine := webrtc.SettingEngine{} // Use the SetNet setting https://pkg.go.dev/github.com/pion/webrtc/v3#SettingEngine.SetNet @@ -102,8 +102,14 @@ func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription, } return nil, err } - // Wait for ICE candidate gathering to complete - <-done + + // Wait for ICE candidate gathering to complete, + // or for whatever we managed to gather before the client times out. + // See https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40230 + select { + case <-done: + case <-time.After(iceGatheringTimeout): + } return pc, nil } @@ -135,7 +141,10 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) { } dataChan := make(chan struct{}) - pc, err := makePeerConnectionFromOffer(stunURL, sdp, dataChan) + // TODO refactor: DRY this must be below `ResponseHeaderTimeout` in proxy + // https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/e1d9b4ace69897521cc29585b5084c5f4d1ce874/proxy/lib/snowflake.go#L207 + iceGatheringTimeout := 10 * time.Second + pc, err := makePeerConnectionFromOffer(stunURL, sdp, dataChan, iceGatheringTimeout) if err != nil { log.Printf("Error making WebRTC connection: %s", err) w.WriteHeader(http.StatusInternalServerError)