`hold()` / `unhold()` terminate the whole call when the re-INVITE gets `491 Request Pending` (glare), or any other error response
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
491should 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:
- roll back the unapplied local offer (
pc.setLocalDescription({ type: 'rollback' })); after the failure it currently stays inhave-local-offer; - restore
_localHoldto its previous value and emit the matchinghold/unholdevent; - report the failure to the application (for example through the
done/failedcallback) instead of callingterminate().
Steps to reproduce
- Establish a call A↔B through a B2BUA.
- Have both sides call
session.hold()at the same instant, or callunhold()on one side while the PBX re-INVITEs that leg. - Both sides get
491. If the single 1 s retry collides again, JsSIP sendsBYEwith "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).
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
- Both ends re-INVITE at once. Our hold re-INVITE gets
491 Request Pending. DialogRequestSender._receiveResponse()schedules the single retry: after 1 s it callssend()again with the same request, i.e. the same SDP offer. Only the CSeq is bumped.- 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 theRTCPeerConnectionis inhave-local-offer, and the browser implicitly rolls back our offer and goes tostable. - The stale re-send is answered
200 OKand ACKed.setRemoteDescription(answer)then fails withInvalidStateError: Failed to set remote answer sdp: Called in wrong state: stable. - Today
hold()turns this intoterminate()("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
- For hold/unhold (and ideally
renegotiate()), don't re-send the stored request after a491. Report the491to 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. - 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.
- 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.