From 0efc2fa707b0b5c2b2514e22d5a955af4f605218 Mon Sep 17 00:00:00 2001
From: Joshua C. Colp <jcolp@sangoma.com>
Date: Wed, 28 Apr 2021 09:36:35 -0300
Subject: [PATCH] res_pjsip_session: Don't produce an offer if no channel is present.

If a re-INVITE is received after we have sent a BYE request then it
is possible for no channel to be present on the session. If this
occurs we allow PJSIP to produce the offer instead. Since the call
is being hung up if it produces an incorrect offer it doesn't
actually matter. This also ensures that code which produces SDP
does not need to handle if a channel is not present.

ASTERISK-29381

Change-Id: I673cb88c432f38f69b2e0851d55cc57a62236042
---

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 3f61b63..b1288b5 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -5263,6 +5263,16 @@
 	pjmedia_sdp_session *offer;
 	int i;
 
+	/* We allow PJSIP to produce an SDP if no channel is present. This may result
+	 * in an incorrect SDP occurring, but if no channel is present then we are in
+	 * the midst of a BYE and are hanging up. This ensures that all the code to
+	 * produce an SDP doesn't need to worry about a channel being present or not,
+	 * just in case.
+	 */
+	if (!session->channel) {
+		return;
+	}
+
 	if (inv->neg) {
 		if (pjmedia_sdp_neg_was_answer_remote(inv->neg)) {
 			pjmedia_sdp_neg_get_active_remote(inv->neg, &previous_sdp);
