Skip to content

iceGatheringTimeout acts oddly #1126

Open

Description

@nnew2

We are experiencing an artificial delay in our call setup when using SIP.js with an Asterisk backend over a WebSockets transport.

We recently noticed that when configuring a high iceGatheringTimeout value (e.g., 5000 ms), SIP.js holds back the SIP signaling (INVITE / 200 OK) for exactly that duration, even if the underlying browser engine has already finished gathering all viable ICE candidates within milliseconds.

The Behavior We See (From chrome://webrtc-internals):
When analyzing our WebRTC dumps, we see a clean, immediate gathering process from the browser, but an exact 5-second stall before the SIP layer reacts:

14:09:58.621 – Gathering starts (iceTransportPolicy: "all").
14:09:58.709 – The browser finishes gathering local and STUN candidates. The state transitions to iceGatheringState: "complete" (taking only ~88ms total).
The Stall – The browser sits idle, waiting.
14:10:03.621 – Exactly 5000ms after gathering started, the session description handler flushes.
14:10:03.714 – The SIP message goes out, and the connection transitions to connected instantly.

If we lower iceGatheringTimeout to 1000, the call delays for exactly 1000ms. If we set it to 750, it delays for exactly 750ms.

Based on standard WebRTC behavior, the iceGatheringTimeout should act as an upper ceiling (maximum timeout) to abort a stalled gathering process. If the browser fires an explicit iceGatheringState === 'complete' event before the timeout expires, the application should theoretically resolve the SDP and proceed immediately.

Instead, it appears that SIP.js is treating iceGatheringTimeout as a mandatory, blocking setTimeout window.

Is this expected? Is there a work-around for this, configuration etc.?

Activity

  1. ningning54321 commented on Nov 26, 2026

    @ningning54321

    Mostly yes. In SIP.js, waitForIceGatheringComplete only exits when ICE gathering completes or iceGatheringTimeout expires (5 s by default). SIP.js doesn't support Trickle ICE, so it holds the INVITE or 200 OK until it has the full candidate set. In your trace the browser reported complete after about 88 ms, but the SDH didn't release until the timer fired. So in practice the timeout acts as the delay rather than a ceiling. We see the same behavior and handle it with the workarounds below.

    Workarounds in my mind:

    1. As you have tested, lower iceGatheringTimeout. This is the simplest option. In our testing, 500 ms to 1 s works for most networks. A value that's too short may leave you with no valid candidate.
    2. Force completion from onicecandidate (what we use). Once the first srflx candidate arrives, call the SDH's iceGatheringComplete() yourself. This is usually very fast.
    session.delegate = {
      onSessionDescriptionHandler: (sdh) => {
        if (!(sdh instanceof SessionDescriptionHandler)) return;
        sdh.peerConnectionDelegate = {
          onicecandidate: (event) => {
            if (event.candidate?.type === 'srflx') {
              (sdh as any).iceGatheringComplete(); // stop waiting
            }
          }
        };
      }
    };
    
    1. Disable STUN for the initial offer. Set iceServers to empty so only host candidates are gathered, which is quick. The peer's 200 OK returns its candidates, and the client's STUN binding request to them lets the peer learn the client's public IP and port. This only works if the peer is publicly reachable (as with an Asterisk server). You'll need TURN if clients sit behind symmetric NAT or restrictive firewalls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions