<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15122">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Sean Bright: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Joshua Colp: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pjsip: Match lifetime of INVITE session to our session.<br><br>In some circumstances it was possible for an INVITE<br>session to be destroyed while we were still using it.<br>This occurred due to the reference on the INVITE session<br>being released internally as a result of its state<br>changing to DISCONNECTED.<br><br>This change adds a reference to the INVITE session<br>which is released when our own session is destroyed,<br>ensuring that the INVITE session remains valid for<br>the lifetime of our session.<br><br>ASTERISK-29022<br><br>Change-Id: I300c6d9005ff0e6efbe1132daefc7e47ca6228c9<br>---<br>M channels/chan_pjsip.c<br>M res/res_pjsip_session.c<br>2 files changed, 37 insertions(+), 149 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/channels/chan_pjsip.c b/channels/chan_pjsip.c</span><br><span>index 1c10fbf..649dd36 100644</span><br><span>--- a/channels/chan_pjsip.c</span><br><span>+++ b/channels/chan_pjsip.c</span><br><span>@@ -673,9 +673,6 @@</span><br><span> ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",</span><br><span> session->inv_session->cause,</span><br><span> pjsip_get_status_text(session->inv_session->cause)->ptr);</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -692,10 +689,6 @@</span><br><span> ast_sip_session_send_response(session, packet);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> if (status != PJ_SUCCESS) {</span><br><span> char err[PJ_ERR_MSG_SIZE];</span><br><span> </span><br><span>@@ -725,14 +718,6 @@</span><br><span> ast_setstate(ast, AST_STATE_UP);</span><br><span> session = ao2_bump(channel->session);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(session, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /* the answer task needs to be pushed synchronously otherwise a race condition</span><br><span> can occur between this thread and bridging (specifically when native bridging</span><br><span> attempts to do direct media) */</span><br><span>@@ -742,9 +727,6 @@</span><br><span> if (res == -1) {</span><br><span> ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",</span><br><span> ast_channel_name(session->channel));</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> }</span><br><span> ao2_ref(session, -1);</span><br><span> ast_channel_lock(ast);</span><br><span>@@ -1343,9 +1325,6 @@</span><br><span> ast_sip_session_send_response(session, packet);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_ref(ind_data, -1);</span><br><span> </span><br><span> return 0;</span><br><span>@@ -1377,31 +1356,20 @@</span><br><span> ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",</span><br><span> session->inv_session->cause,</span><br><span> pjsip_get_status_text(session->inv_session->cause)->ptr);</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> </span><br><span> if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {</span><br><span> ast_log(LOG_ERROR, "Could not create text video update INFO request\n");</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> if (ast_sip_add_body(tdata, &body)) {</span><br><span> ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> ast_sip_session_send_request(session, tdata);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-failure:</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span>@@ -1449,9 +1417,6 @@</span><br><span> ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",</span><br><span> session->inv_session->cause,</span><br><span> pjsip_get_status_text(session->inv_session->cause)->ptr);</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_ref(session, -1);</span><br><span> return -1;</span><br><span> }</span><br><span>@@ -1492,10 +1457,6 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> ao2_ref(session, -1);</span><br><span> return 0;</span><br><span> }</span><br><span>@@ -1726,18 +1687,9 @@</span><br><span> res = ast_rtp_instance_write(media->rtp, &fr);</span><br><span> } else {</span><br><span> ao2_ref(channel->session, +1);</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {</span><br><span> ao2_cleanup(channel->session);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(channel->session);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> }</span><br><span> ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");</span><br><span> } else {</span><br><span>@@ -1751,17 +1703,7 @@</span><br><span> break;</span><br><span> case AST_CONTROL_CONNECTED_LINE:</span><br><span> ao2_ref(channel->session, +1);</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(channel->session);</span><br><span style="color: hsl(0, 100%, 40%);">- SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't increase the session reference counter\n",</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_name(ast));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(channel->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_cleanup(channel->session);</span><br><span> }</span><br><span> break;</span><br><span>@@ -1868,18 +1810,10 @@</span><br><span> if (!ind_data) {</span><br><span> SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc indicate data\n", ast_channel_name(ast));</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(ind_data->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(ind_data);</span><br><span style="color: hsl(0, 100%, 40%);">- SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't increase the session reference counter\n", ast_channel_name(ast));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {</span><br><span> ast_log(LOG_ERROR, "%s: Cannot send response code %d to endpoint %s. Could not queue task properly\n",</span><br><span> ast_channel_name(ast), response_code, ast_sorcery_object_get_id(channel->session->endpoint));</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(ind_data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_cleanup(ind_data);</span><br><span> res = -1;</span><br><span> }</span><br><span>@@ -2150,10 +2084,6 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(trnf_data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> ao2_ref(trnf_data, -1);</span><br><span> ao2_cleanup(endpoint);</span><br><span> ao2_cleanup(contact);</span><br><span>@@ -2170,19 +2100,8 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(trnf_data->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(trnf_data);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {</span><br><span> ast_log(LOG_WARNING, "Error requesting transfer\n");</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(trnf_data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_cleanup(trnf_data);</span><br><span> return -1;</span><br><span> }</span><br><span>@@ -2277,12 +2196,12 @@</span><br><span> ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",</span><br><span> session->inv_session->cause,</span><br><span> pjsip_get_status_text(session->inv_session->cause)->ptr);</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> </span><br><span> if (!(body_text = ast_str_create(32))) {</span><br><span> ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);</span><br><span> </span><br><span>@@ -2290,27 +2209,16 @@</span><br><span> </span><br><span> if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {</span><br><span> ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> if (ast_sip_add_body(tdata, &body)) {</span><br><span> ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");</span><br><span> pjsip_tx_data_dec_ref(tdata);</span><br><span style="color: hsl(0, 100%, 40%);">- goto failure;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> ast_sip_session_send_request(session, tdata);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-failure:</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> }</span><br><span> </span><br><span> /*! \brief Function called by core to stop a DTMF digit */</span><br><span>@@ -2351,19 +2259,8 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(dtmf_data->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(dtmf_data);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {</span><br><span> ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(dtmf_data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_cleanup(dtmf_data);</span><br><span> return -1;</span><br><span> }</span><br><span>@@ -2858,10 +2755,6 @@</span><br><span> ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> ao2_cleanup(data);</span><br><span> </span><br><span> return 0;</span><br><span>@@ -2883,18 +2776,7 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(data->session->inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(data, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(data->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> ao2_ref(data, -1);</span><br><span> return -1;</span><br><span> }</span><br><span>diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c</span><br><span>index 64b92dd..34bd597 100644</span><br><span>--- a/res/res_pjsip_session.c</span><br><span>+++ b/res/res_pjsip_session.c</span><br><span>@@ -2979,7 +2979,16 @@</span><br><span> ast_dsp_free(session->dsp);</span><br><span> </span><br><span> if (session->inv_session) {</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_dlg_dec_session(session->inv_session->dlg, &session_module);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct pjsip_dialog *dlg = session->inv_session->dlg;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* The INVITE session uses the dialog pool for memory, so we need to</span><br><span style="color: hsl(120, 100%, 40%);">+ * decrement its reference first before that of the dialog.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_inv_dec_ref(session->inv_session);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_dlg_dec_session(dlg, &session_module);</span><br><span> }</span><br><span> </span><br><span> ast_test_suite_event_notify("SESSION_DESTROYED", "Endpoint: %s", endpoint_name);</span><br><span>@@ -3087,6 +3096,24 @@</span><br><span> }</span><br><span> ast_sip_dialog_set_serializer(inv_session->dlg, session->serializer);</span><br><span> ast_sip_dialog_set_endpoint(inv_session->dlg, endpoint);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* When a PJSIP INVITE session is created it is created with a reference</span><br><span style="color: hsl(120, 100%, 40%);">+ * count of 1, with that reference being managed by the underlying state</span><br><span style="color: hsl(120, 100%, 40%);">+ * of the INVITE session itself. When the INVITE session transitions to</span><br><span style="color: hsl(120, 100%, 40%);">+ * a DISCONNECTED state that reference is released. This means we can not</span><br><span style="color: hsl(120, 100%, 40%);">+ * rely on that reference to ensure the INVITE session remains for the</span><br><span style="color: hsl(120, 100%, 40%);">+ * lifetime of our session. To ensure it does we add our own reference</span><br><span style="color: hsl(120, 100%, 40%);">+ * and release it when our own session goes away, ensuring that the INVITE</span><br><span style="color: hsl(120, 100%, 40%);">+ * session remains for the lifetime of session.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> pjsip_dlg_inc_session(inv_session->dlg, &session_module);</span><br><span> inv_session->mod_data[session_module.id] = ao2_bump(session);</span><br><span> session->contact = ao2_bump(contact);</span><br><span>@@ -3962,9 +3989,6 @@</span><br><span> ast_sip_session_get_name(invite->session),</span><br><span> invite->session->inv_session->cause,</span><br><span> pjsip_get_status_text(invite->session->inv_session->cause)->ptr);</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(invite->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> SCOPE_EXIT_RTN_VALUE(-1);</span><br><span> }</span><br><span> </span><br><span>@@ -4082,9 +4106,6 @@</span><br><span> handle_incoming_request(invite->session, invite->rdata);</span><br><span> </span><br><span> end:</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(invite->session->inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(invite->session));</span><br><span> }</span><br><span> </span><br><span>@@ -4127,17 +4148,6 @@</span><br><span> * process handling has successfully completed.</span><br><span> */</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");</span><br><span style="color: hsl(0, 100%, 40%);">- /* Dialog's lock and a reference are removed in new_invite_initial_answer */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!new_invite_initial_answer(inv_session, rdata, 500, 500, PJ_FALSE)) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Terminate the session if it wasn't done in the answer */</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_terminate(inv_session, 500, PJ_FALSE);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> session = ast_sip_session_alloc(endpoint, NULL, inv_session, rdata);</span><br><span> if (!session) {</span><br><span> /* Dialog's lock and reference are removed in new_invite_initial_answer */</span><br><span>@@ -4145,10 +4155,6 @@</span><br><span> /* Terminate the session if it wasn't done in the answer */</span><br><span> pjsip_inv_terminate(inv_session, 500, PJ_FALSE);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-#ifdef HAVE_PJSIP_INV_SESSION_REF</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_inv_dec_ref(inv_session);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15122">change 15122</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/15122"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I300c6d9005ff0e6efbe1132daefc7e47ca6228c9 </div>
<div style="display:none"> Gerrit-Change-Number: 15122 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>