From 2bd65a098fceaed0fbbb03398d17f462b9245efc Mon Sep 17 00:00:00 2001
From: Iryna Antsyferova <iryna.antsyferova@iopsys.eu>
Date: Wed, 11 Jun 2025 12:39:34 +0000
Subject: [PATCH] Flush pending transmissions to finalize previous TLS
 sessions, REF 16834

This commit fixes the case when Asterisk cycles through SIP servers one by one, trying to
register with TLS transport. The problem is that SIP-TLS remains active and further Register
requests are sent to the previous P-CSCF as well. pjproject has tls->delayed_list for
transmissions that were not successful. Note that these transmissions should remain for 32
seconds (configured in pjsip.conf timer_b = 32000). However, it seems like they remain for a
longer time (forever?). This commit flushes delayed TXs.
---
 ...023-Flush_pending_tx_at_tls_shutdown.patch | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 third-party/pjproject/patches/0023-Flush_pending_tx_at_tls_shutdown.patch

diff --git a/third-party/pjproject/patches/0023-Flush_pending_tx_at_tls_shutdown.patch b/third-party/pjproject/patches/0023-Flush_pending_tx_at_tls_shutdown.patch
new file mode 100644
index 0000000000..6654435af6
--- /dev/null
+++ b/third-party/pjproject/patches/0023-Flush_pending_tx_at_tls_shutdown.patch
@@ -0,0 +1,40 @@
+diff --git a/pjsip/src/pjsip/sip_transport_tls.c b/pjsip/src/pjsip/sip_transport_tls.c
+index 2c969a31c..73917945e 100644
+--- a/pjsip/src/pjsip/sip_transport_tls.c
++++ b/pjsip/src/pjsip/sip_transport_tls.c
+@@ -175,6 +175,7 @@ static void tls_on_destroy(void *arg);
+ 
+ static void wipe_buf(pj_str_t *buf);
+ 
++static void tls_flush_pending_tx(struct tls_transport *tls);
+ 
+ static void tls_perror(const char *sender, const char *title,
+                        pj_status_t status, pj_str_t *remote_name)
+@@ -244,7 +245,7 @@ static void tls_init_shutdown(struct tls_transport *tls, pj_status_t status)
+         tls->close_reason = status;
+ 
+     if (tls->base.is_shutdown || tls->base.is_destroying)
+-        return;
++        goto End;
+ 
+     /* Prevent immediate transport destroy by application, as transport
+      * state notification callback may be stacked and transport instance
+@@ -277,7 +278,7 @@ static void tls_init_shutdown(struct tls_transport *tls, pj_status_t status)
+     /* check again */
+     if (tls->base.is_shutdown || tls->base.is_destroying) {
+         pjsip_transport_dec_ref(&tls->base);
+-        return;
++        goto End;
+     }
+ 
+     /* We can not destroy the transport since high level objects may
+@@ -289,6 +290,9 @@ static void tls_init_shutdown(struct tls_transport *tls, pj_status_t status)
+ 
+     /* Now, it is ok to destroy the transport. */
+     pjsip_transport_dec_ref(&tls->base);
++
++End:
++    tls_flush_pending_tx(tls);
+ }
+ 
+ 
-- 
GitLab