Commit 80b9814 for jssip.net

commit 80b9814aeff941a978573a8cd6b584fb49ac296f
Author: José Luis Millán <jmillan@aliax.net>
Date:   Thu Dec 3 17:23:34 2026 +0100

    Transactions.isPending()

diff --git a/src/Transactions.ts b/src/Transactions.ts
index 5d187ba..c6d2cf1 100644
--- a/src/Transactions.ts
+++ b/src/Transactions.ts
@@ -772,6 +772,38 @@ export class InviteServerTransaction extends EventEmitter {
  *  _true_  retransmission
  *  _false_ new request
  */
+/**
+ * Whether the transaction is still waiting for its answer.
+ *
+ * @remarks
+ * - A transaction that already has its final response stays alive only to
+ *   absorb retransmissions of it. An INVITE client transaction holds
+ *   'accepted' for Timer M, 32 seconds
+ *   ([RFC 6026, Section 7.2](https://datatracker.ietf.org/doc/html/rfc6026#section-7.2)),
+ *   long after the exchange it carried is over.
+ * - An INVITE server transaction reaches 'accepted' with its ACK still to
+ *   come, and holds it for Timer L whether or not the ACK then arrives, so
+ *   that state says nothing about whether anything is outstanding either.
+ * - `UA.stop()` reads this to tell whether the requests it has just sent
+ *   still stand a chance of being answered, instead of waiting out its
+ *   whole close timeout.
+ */
+export function isPending(transaction: Transaction): boolean {
+	switch (transaction.state) {
+		// A client transaction has no state until it is sent.
+		case undefined:
+		case Status.TRYING:
+		case Status.PROCEEDING:
+		case Status.CALLING: {
+			return true;
+		}
+
+		default: {
+			return false;
+		}
+	}
+}
+
 export function checkTransaction(
 	{ transactions }: { transactions: TransactionRegistry },
 	request: SIPMessage.IncomingRequest
diff --git a/src/UA.ts b/src/UA.ts
index 413e1af..dd0c91b 100644
--- a/src/UA.ts
+++ b/src/UA.ts
@@ -1208,15 +1208,27 @@ export class UA extends EventEmitter<UAEventMap> {
 	 * here with none of its own once its dialog is confirmed. Subscribers and
 	 * applicants are not: each is destroyed while handling the response to its
 	 * last request, a tick before the transaction that carried it.
+	 *
+	 * A transaction only counts while {@link Transactions.isPending} says it
+	 * is still waiting for its answer. A usage with something left to send
+	 * keeps itself registered instead, which the session check covers.
 	 */
 	#isIdle(): boolean {
-		return (
-			Object.keys(this.#sessions).length === 0 &&
-			Object.keys(this.transactions.nict).length === 0 &&
-			Object.keys(this.transactions.nist).length === 0 &&
-			Object.keys(this.transactions.ict).length === 0 &&
-			Object.keys(this.transactions.ist).length === 0
-		);
+		if (Object.keys(this.#sessions).length !== 0) {
+			return false;
+		}
+
+		for (const table of Object.values(this.transactions)) {
+			for (const transaction of Object.values<Transactions.Transaction>(
+				table
+			)) {
+				if (Transactions.isPending(transaction)) {
+					return false;
+				}
+			}
+		}
+
+		return true;
 	}

 	/**
diff --git a/src/test/test-RTCSession.ts b/src/test/test-RTCSession.ts
index feaf5b9..5bcbbc6 100644
--- a/src/test/test-RTCSession.ts
+++ b/src/test/test-RTCSession.ts
@@ -328,6 +328,28 @@ class CallerSocket extends TestSocket {
 		);
 	}

+	// Whether to hold the ACK back, so that a test can act while the answer
+	// is still unacknowledged.
+	private withholdAck = false;
+
+	/**
+	 * Stops acknowledging the answer, and gives back the ACK that would have
+	 * been sent so a test can let it through later.
+	 */
+	holdAckBack(): () => void {
+		this.withholdAck = true;
+
+		return () => {
+			this.withholdAck = false;
+
+			const ok = this.responses('INVITE').find(r => status(r) === 200);
+
+			if (ok) {
+				this.onResponse(ok);
+			}
+		};
+	}
+
 	/**
 	 * Hangs the established call up.
 	 */
@@ -351,7 +373,11 @@ class CallerSocket extends TestSocket {

 	protected override onResponse(response: string): void {
 		// Acknowledging the answer is what confirms the dialog.
-		if (method(response) === 'INVITE' && status(response) === 200) {
+		if (
+			!this.withholdAck &&
+			method(response) === 'INVITE' &&
+			status(response) === 200
+		) {
 			this.toTag = tag(header(response, 'To'));

 			this.receive(
@@ -621,6 +647,39 @@ describe('RTCSession', () => {
 		expect(end).toMatchObject({ originator: 'remote', cause: C.causes.BYE });
 	});

+	test('hanging up before the ACK holds the BYE back until it arrives', async () => {
+		const socket = new CallerSocket();
+		const { ua, session } = await receiveCall(socket);
+
+		const letAckThrough = socket.holdAckBack();
+
+		session.answer({ mediaConstraints: { audio: true, video: false } });
+
+		await once(session, 'accepted');
+
+		// RFC 3261, Section 15: the callee must not send a BYE on a confirmed
+		// dialog before the ACK to its 2xx has arrived.
+		session.terminate();
+
+		await delay(50);
+
+		const beforeAck = socket.requests('BYE').length;
+
+		const stopped = stopUA(ua);
+
+		letAckThrough();
+
+		await stopped;
+
+		expect(beforeAck).toBe(0);
+
+		// The BYE never follows, because terminating the session took the
+		// dialog down with it and the ACK no longer matches anything. See the
+		// entry in TODO.md: this expectation should become 1 once that is
+		// fixed.
+		expect(socket.requests('BYE')).toHaveLength(0);
+	});
+
 	test('an incoming call the application rejects', async () => {
 		const socket = new CallerSocket();
 		const { ua, session, events } = await receiveCall(socket);