<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13779">View Change</a></p><div style="white-space:pre-wrap">Approvals:
George Joseph: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved
Joshua Colp: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_session: Fix off-nominal session refreshes.<br><br>Given a scenario where session refreshes occur close to<br>each other while another is finishing it was possible for<br>the session refreshes to occur out of order. It was<br>also possible for session refreshes to be delayed for<br>quite some time if a session refresh did not result in<br>a topology change.<br><br>For the out of order session refreshes the first session<br>refresh would be queued due to a transaction in progress.<br>This transaction would then finish. When finished a<br>separate task to process the delayed requests queue<br>would be queued for handling. A second refresh would<br>be requested internally before this delayed request<br>queued task was processed. As no transaction was in<br>progress this session refresh would be immediately<br>handled before the queued session refresh.<br><br>The code will now check if any delayed requests exist<br>before allowing a session refresh to immediately occur.<br>If any exist then the session refresh is queued.<br><br>For the delayed session refreshes if a session refresh<br>did not result in a topology change the attempt would<br>be immediately stopped and no other delayed requests would<br>be processed.<br><br>The code will now go through the entire delayed requests<br>queue until a delayed request results in a request<br>actually being sent.<br><br>ASTERISK-28730<br><br>Change-Id: Ied640280133871f77d3f332be62265e754605088<br>---<br>M res/res_pjsip_session.c<br>1 file changed, 62 insertions(+), 11 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c</span><br><span>index 76740c1..0eafb9c 100644</span><br><span>--- a/res/res_pjsip_session.c</span><br><span>+++ b/res/res_pjsip_session.c</span><br><span>@@ -69,6 +69,13 @@</span><br><span> enum ast_sip_session_response_priority response_priority);</span><br><span> static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata);</span><br><span> static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata);</span><br><span style="color: hsl(120, 100%, 40%);">+static int sip_session_refresh(struct ast_sip_session *session,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_request_creation_cb on_request_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_sdp_creation_cb on_sdp_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_response_cb on_response,</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_sip_session_refresh_method method, int generate_new_sdp,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_media_state *media_state,</span><br><span style="color: hsl(120, 100%, 40%);">+ int queued);</span><br><span> </span><br><span> /*! \brief NAT hook for modifying outgoing messages with SDP */</span><br><span> static struct ast_sip_nat_hook *nat_hook;</span><br><span>@@ -1244,8 +1251,18 @@</span><br><span> ast_free(delay);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Send a delayed request</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 failure</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 1 refresh request not sent as no change would occur</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> static int send_delayed_request(struct ast_sip_session *session, struct ast_sip_session_delayed_request *delay)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ int res;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ast_debug(3, "Endpoint '%s(%s)' sending delayed %s request.\n",</span><br><span> ast_sorcery_object_get_id(session->endpoint),</span><br><span> session->channel ? ast_channel_name(session->channel) : "",</span><br><span>@@ -1253,19 +1270,19 @@</span><br><span> </span><br><span> switch (delay->method) {</span><br><span> case DELAYED_METHOD_INVITE:</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_session_refresh(session, delay->on_request_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ res = sip_session_refresh(session, delay->on_request_creation,</span><br><span> delay->on_sdp_creation, delay->on_response,</span><br><span style="color: hsl(0, 100%, 40%);">- AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp, delay->media_state);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp, delay->media_state, 1);</span><br><span> /* Ownership of media state transitions to ast_sip_session_refresh */</span><br><span> delay->media_state = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span> case DELAYED_METHOD_UPDATE:</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_session_refresh(session, delay->on_request_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ res = sip_session_refresh(session, delay->on_request_creation,</span><br><span> delay->on_sdp_creation, delay->on_response,</span><br><span style="color: hsl(0, 100%, 40%);">- AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp, delay->media_state);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp, delay->media_state, 1);</span><br><span> /* Ownership of media state transitions to ast_sip_session_refresh */</span><br><span> delay->media_state = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span> case DELAYED_METHOD_BYE:</span><br><span> ast_sip_session_terminate(session, 0);</span><br><span> return 0;</span><br><span>@@ -1300,7 +1317,9 @@</span><br><span> AST_LIST_REMOVE_CURRENT(next);</span><br><span> res = send_delayed_request(session, delay);</span><br><span> delayed_request_free(delay);</span><br><span style="color: hsl(0, 100%, 40%);">- found = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!res) {</span><br><span style="color: hsl(120, 100%, 40%);">+ found = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> break;</span><br><span> case DELAYED_METHOD_BYE:</span><br><span> /* A BYE is pending so don't bother anymore. */</span><br><span>@@ -1354,7 +1373,9 @@</span><br><span> AST_LIST_REMOVE_CURRENT(next);</span><br><span> res = send_delayed_request(session, delay);</span><br><span> delayed_request_free(delay);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!res) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> }</span><br><span> AST_LIST_TRAVERSE_SAFE_END;</span><br><span>@@ -1558,12 +1579,13 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-int ast_sip_session_refresh(struct ast_sip_session *session,</span><br><span style="color: hsl(120, 100%, 40%);">+static int sip_session_refresh(struct ast_sip_session *session,</span><br><span> ast_sip_session_request_creation_cb on_request_creation,</span><br><span> ast_sip_session_sdp_creation_cb on_sdp_creation,</span><br><span> ast_sip_session_response_cb on_response,</span><br><span> enum ast_sip_session_refresh_method method, int generate_new_sdp,</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_media_state *media_state)</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_media_state *media_state,</span><br><span style="color: hsl(120, 100%, 40%);">+ int queued)</span><br><span> {</span><br><span> pjsip_inv_session *inv_session = session->inv_session;</span><br><span> pjmedia_sdp_session *new_sdp = NULL;</span><br><span>@@ -1630,6 +1652,20 @@</span><br><span> int type_streams[AST_MEDIA_TYPE_END] = {0};</span><br><span> struct ast_stream *stream;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* Media state conveys a desired media state, so if there are outstanding</span><br><span style="color: hsl(120, 100%, 40%);">+ * delayed requests we need to ensure we go into the queue and not jump</span><br><span style="color: hsl(120, 100%, 40%);">+ * ahead. If we sent this media state now then updates could go out of</span><br><span style="color: hsl(120, 100%, 40%);">+ * order.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!queued && !AST_LIST_EMPTY(&session->delayed_requests)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Delay sending reinvite to %s because of outstanding requests...\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_get_id(session->endpoint));</span><br><span style="color: hsl(120, 100%, 40%);">+ return delay_request(session, on_request_creation, on_sdp_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ on_response, generate_new_sdp,</span><br><span style="color: hsl(120, 100%, 40%);">+ method == AST_SIP_SESSION_REFRESH_METHOD_INVITE</span><br><span style="color: hsl(120, 100%, 40%);">+ ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, media_state);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Prune the media state so the number of streams fit within the configured limits - we do it here</span><br><span> * so that the index of the resulting streams in the SDP match. If we simply left the streams out</span><br><span> * of the SDP when producing it we'd be in trouble. We also enforce formats here for media types that</span><br><span>@@ -1742,7 +1778,11 @@</span><br><span> /* If the resulting media state matches the existing active state don't bother doing a session refresh */</span><br><span> if (ast_stream_topology_equal(session->active_media_state->topology, media_state->topology)) {</span><br><span> ast_sip_session_media_state_free(media_state);</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* For external consumers we return 0 to say success, but internally for</span><br><span style="color: hsl(120, 100%, 40%);">+ * send_delayed_request we return a separate value to indicate that this</span><br><span style="color: hsl(120, 100%, 40%);">+ * session refresh would be redundant so we didn't send it</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ return queued ? 1 : 0;</span><br><span> }</span><br><span> }</span><br><span> </span><br><span>@@ -1794,6 +1834,17 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_sip_session_refresh(struct ast_sip_session *session,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_request_creation_cb on_request_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_sdp_creation_cb on_sdp_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_response_cb on_response,</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_sip_session_refresh_method method, int generate_new_sdp,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_media_state *media_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return sip_session_refresh(session, on_request_creation, on_sdp_creation,</span><br><span style="color: hsl(120, 100%, 40%);">+ on_response, method, generate_new_sdp, media_state, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_sip_session_regenerate_answer(struct ast_sip_session *session,</span><br><span> ast_sip_session_sdp_creation_cb on_sdp_creation)</span><br><span> {</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13779">change 13779</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/+/13779"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Ied640280133871f77d3f332be62265e754605088 </div>
<div style="display:none"> Gerrit-Change-Number: 13779 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </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-MessageType: merged </div>