JsSIP icon indicating copy to clipboard operation
JsSIP copied to clipboard

`hold()` / `unhold()` terminate the whole call when the re-INVITE gets `491 Request Pending` (glare), or any other error response

Open deltapath-eric opened this issue 1 month ago • 1 comments

Version

JsSIP 3.13.8 (npm), Chrome.

Description

RTCSession.hold() and unhold() pass an eventHandlers.failed that terminates the session on any failure of the re-INVITE:

lib/RTCSession.js:

// hold()
failed: () => {
    this.terminate({
        cause: JsSIP_C.causes.WEBRTC_ERROR,
        status_code: 500,
        reason_phrase: 'Hold Failed',
    });
},
// unhold(): same, with reason_phrase 'Unhold Failed'

renegotiate() does the same with 'Media Renegotiation Failed'.

That includes 491 Request Pending. DialogRequestSender retries a 491 exactly once, after a fixed 1 s. If that retry also collides, onErrorResponse fires, failed runs, and the call is torn down with BYE and Reason: SIP;cause=500;text="Unhold Failed".

Glare isn't rare with a B2BUA. Examples:

  • Swap: the user resumes call 1, which holds call 2, while the far end of call 1 is also changing hold.
  • Simultaneous hold: both parties press Hold at roughly the same time.
  • PBX re-INVITE: the PBX re-INVITEs the leg (music on hold, bridging) just as the client sends its own hold or unhold.

In our tests, simultaneous hold/resume dropped the call reliably.

Expected (per RFC 3261 §14.1)

  • A 491 should be retried after a randomised delay: 2.1–4 s if the UA owns the Call-ID, 0–2 s otherwise, rather than a fixed 1 s with a single attempt.
  • A non-2xx final response to a re-INVITE should leave the session as it was, "as if no re-INVITE had been issued". The session shouldn't be terminated.

Concretely, on a failed hold/unhold re-INVITE JsSIP should:

  1. roll back the unapplied local offer (pc.setLocalDescription({ type: 'rollback' })); after the failure it currently stays in have-local-offer;
  2. restore _localHold to its previous value and emit the matching hold/unhold event;
  3. report the failure to the application (for example through the done/failed callback) instead of calling terminate().

Steps to reproduce

  1. Establish a call A↔B through a B2BUA.
  2. Have both sides call session.hold() at the same instant, or call unhold() on one side while the PBX re-INVITEs that leg.
  3. Both sides get 491. If the single 1 s retry collides again, JsSIP sends BYE with "Hold Failed"/"Unhold Failed" and the call ends.

Workaround

We wrap hold()/unhold() per session and intercept the failed handler passed to _sendReinvite/_sendUpdate. On failure we roll back the local offer. On 491 we retry once more after the §14.1 delay. Otherwise we restore _localHold and emit the opposite event instead of terminating. This relies on private APIs, so a built-in fix would be much better.

Related

The 491 re-attempt is also sent after the call has ended, because Dialog.isTerminated() never returns true (separate issue).

deltapath-eric avatar Oct 20 '26 02:10 deltapath-eric

Update: the 1 s retry re-sends a stale offer, and "restore on failure" can desync hold state

Further testing against a B2BUA (JsSIP 3.13.8, Chrome) found a second failure mode in the same area. It changes the proposed fix.

What happens

  1. Both ends re-INVITE at once. Our hold re-INVITE gets 491 Request Pending.
  2. DialogRequestSender._receiveResponse() schedules the single retry: after 1 s it calls send() again with the same request, i.e. the same SDP offer. Only the CSeq is bumped.
  3. During that second, the far end retries its re-INVITE. Our transaction is no longer pending, so JsSIP accepts it. setRemoteDescription(offer) is called while the RTCPeerConnection is in have-local-offer, and the browser implicitly rolls back our offer and goes to stable.
  4. The stale re-send is answered 200 OK and ACKed. setRemoteDescription(answer) then fails with InvalidStateError: Failed to set remote answer sdp: Called in wrong state: stable.
  5. Today hold() turns this into terminate() ("Hold Failed"). With the fix proposed above (roll back + restore the previous hold state), the call survives, but the far end has accepted the hold while the local side reports un-held.

Revised proposal

  1. For hold/unhold (and ideally renegotiate()), don't re-send the stored request after a 491. Report the 491 to the session, which then retries with a fresh offer created from the current state, after the RFC 3261 §14.1 delay (2.1–4 s if we own the Call-ID, otherwise 0–2 s), with a bounded number of attempts.
  2. On failure, roll back an unapplied local offer.
    • If there was a SIP error response (non-2xx), restore the previous hold state, emit the matching event, and report the failure to the application.
    • If the 2xx was received and ACKed but the answer couldn't be applied, re-offer the current hold state to resync. Don't restore locally.
  3. Never terminate() because of a hold/unhold/renegotiate failure.

Workaround we ship (for reference): we wrap the session's hold/unhold/_sendReinvite and patch DialogRequestSender.prototype._receiveResponse to report the 491 immediately for those re-INVITEs. The wrapper then rolls back, waits, and retries with a fresh offer (up to 3 attempts), re-offering to resync when the answer can't be applied. With it, simultaneous hold/resume and the subsequent blind and attended transfers pass consistently. It depends on private fields (_dialog._owner, _localHold, _connectionPromiseQueue), so a proper fix in JsSIP would be much better.

deltapath-eric avatar Oct 20 '26 03:10 deltapath-eric