JsSIP icon indicating copy to clipboard operation
JsSIP copied to clipboard

`Dialog.isTerminated()` never returns true (checks `_status`, but `terminate()` sets `_state`), so the 491 re-attempt is sent after the call has ended

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

Version

JsSIP 3.13.8 (npm), Chrome.

Description

Dialog.terminate() marks the dialog as terminated in this._state, but Dialog.isTerminated() reads this._status, which is never set. So isTerminated() always returns false.

lib/Dialog.js (3.13.8):

isTerminated() {
    return this._status === C.STATUS_TERMINATED;   // reads _status
}
...
terminate() {
    logger.debug(`dialog ${this._id.toString()} deleted`);
    this._ua.destroyDialog(this);
    this._state = C.STATUS_TERMINATED;             // writes _state
}

(update() also writes this._state = C.STATUS_CONFIRMED, so _state is clearly the intended field.)

Impact

DialogRequestSender relies on isTerminated() to cancel its RFC 3261 §14.1 re-attempt after a 491 Request Pending.

lib/Dialog/RequestSender.js:

else if (response.method === JsSIP_C.INVITE && response.status_code === 491) {
    if (this._reattempt) {
        this._eventHandlers.onErrorResponse(response);
    } else {
        this._request.cseq = this._dialog.local_seqnum += 1;
        this._reattemptTimer = setTimeout(() => {
            if (!this._dialog.isTerminated()) {   // always true -> always re-sends
                this._reattempt = true;
                this.send();
            }
        }, 1000);
    }
}

If the call is hung up during that 1 s window, the re-INVITE is still sent after the BYE. Against our PBX (a B2BUA) we observed:

  • the server answering the stray re-INVITE with 500 Server Error;
  • in another case, the server challenging it (401), JsSIP re-sending with credentials, and the PBX treating it as a new outgoing call that rang the other party (a "ghost call").

Steps to reproduce

  1. Establish a call A↔B through a B2BUA.
  2. Trigger re-INVITE glare so A gets 491. For example, A puts the call on hold while the PBX sends its own re-INVITE to A (the far end holds or swaps at the same time).
  3. Within 1 s of the 491, hang up on A (session.terminate()).
  4. Observe A sending the re-INVITE after its BYE.

Expected

After Dialog.terminate(), isTerminated() returns true and no further in-dialog requests are sent.

Suggested fix

isTerminated() {
    return this._state === C.STATUS_TERMINATED;
}

Optionally, terminate() could also clear any pending DialogRequestSender re-attempt timers.

Workaround

We currently patch the prototype at load time:

const original = Dialog.prototype.isTerminated;
Dialog.prototype.isTerminated = function () {
    return this._state === Dialog.C.STATUS_TERMINATED || original.call(this);
};

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

Thanks @deltapath-eric, a new version with the fix has just been released.

jmillan avatar Oct 20 '26 09:10 jmillan