Commit 38171601bd for asterisk.org

commit 38171601bd22871d64ffbc7e04d8a47c75dedbd6
Author: Vladislav Kupyansky <vkupyansky@gmail.com>
Date:   Thu Nov 12 12:33:34 2026 +0400

    res_pjsip_session: Release terminated BYE sessions after received responses

    PJPROJECT can immediately terminate an outgoing BYE transaction when it receives
    a 408 or 481 response. It notifies the application with RX_MSG, then detaches
    the transaction from the dialog. The subsequent transaction timer no longer
    notifies the INVITE session's transaction callback.

    res_pjsip_session excludes BYE from the early disconnected-session cleanup.
    For BYE, session_end_if_disconnected() is only called by the
    TIMER/TRANSPORT_ERROR paths. The RX_MSG path therefore leaves inv->mod_data
    holding the ast_sip_session reference after the call has ended. The retained
    session also holds its related objects and message pools.

    After processing response supplements and the response callback, call the
    existing cleanup helper for an outgoing BYE in TERMINATED. The helper checks
    the disconnected INVITE state, clears the stored reference under the dialog
    lock and schedules the usual completion. Normal 200 responses continue through
    COMPLETED and the existing UDP timer cleanup.

    Validation

    - Asterisk master 7dc7542a950cc64f2838bc0afd372d9263584e99 with bundled
      PJPROJECT 2.17.
    - Native Testsuite regression: one synthetic call each for BYE responses 200,
      408 and 481. Each call must report SESSION_DESTROYING before Asterisk
      shutdown.
    - Unmodified code: all SIPp scenarios complete; 200 is destroyed, while 408 and
      481 miss their destruction events. The regression fails as expected.
    - Patched code: all SIPp scenarios complete and all three sessions are destroyed
      before shutdown. The regression passes.
    - The module builds with -Werror. The earlier Asterisk 22.8.2 / PJPROJECT 2.15.1
      reproduction and allocation measurements also confirm the 408 retention path.

    The corresponding regression test is prepared for the Asterisk Testsuite
    repository under tests/channels/pjsip/bye_response_cleanup.

    AI tools assisted with analysis, code, tests and the English PR description.

    UserNote: Fix PJSIP session leaks after 408 or 481 responses to outgoing BYE.

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index e599e3bb7e..e571cd447d 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -5016,6 +5016,13 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
 		if (cb) {
 			cb(session, e->body.tsx_state.src.rdata);
 		}
+		/* A received 408 or 481 terminates a BYE without a later timer callback. */
+		if (tsx->role == PJSIP_ROLE_UAC
+			&& tsx->method.id == PJSIP_BYE_METHOD
+			&& tsx->state == PJSIP_TSX_STATE_TERMINATED
+			&& session_end_if_disconnected(id, inv)) {
+			SCOPE_EXIT_RTN("Completed BYE\n");
+		}
 		break;
 	case PJSIP_EVENT_TRANSPORT_ERROR:
 	case PJSIP_EVENT_TIMER: