From 217c3a39bdec2a9befc5282c85d716b372b1d03b Mon Sep 17 00:00:00 2001
From: Yalu Zhang <yalu.zhang@iopsys.eu>
Date: Wed, 20 Sep 2023 14:32:53 +0200
Subject: [PATCH] Fix a bug that RTP/RTCP ports are not closed for non-answered
 incoming calls

In ast_sip_session_terminate(), reset both active and pending media to prevent
non-freed resources after a call. Otherwise RTP/RTCP ports won't be closed after
the call in some call scenarios, e.g. a non-answered incoming call. In that case,
there are no active media sessions before the swapping.
---
 res/res_pjsip_session.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 80efa3221b..5aea5394e9 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -3578,7 +3578,13 @@ void ast_sip_session_terminate(struct ast_sip_session *session, int response)
 	 */
 	ast_sip_session_media_stats_save(session, session->active_media_state);
 	SWAP(session->active_media_state, session->pending_media_state);
+	/*
+	 * Reset both active and pending media to prevent non-freed resources after a call. Otherwise RTP/RTCP ports
+	 * won't be closed after the call in some call scenarios, e.g. a non-answered incoming call. In that case,
+	 * there are no active media sessions before the swapping.
+	 */
 	ast_sip_session_media_state_reset(session->pending_media_state);
+	ast_sip_session_media_state_reset(session->active_media_state);
 
 	switch (session->inv_session->state) {
 	case PJSIP_INV_STATE_NULL:
-- 
GitLab