<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13172">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
Benjamin Keith Ford: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">stasis: Don't hold app_registry and session locks unnecessarily<br><br>resource_events:stasis_app_message_handler() was locking the session,<br>then attempting to determine if the app had debug enabled which<br>locked the app_registry container. res_stasis:__stasis_app_register<br>was locking the app_registry container then calling app_update<br>which caused app_handler (which locks the session) to run.<br>The result was a deadlock.<br><br>* Updated resource_events:stasis_app_message_handler() to determine<br> if debug was set (which locks the app_registry) before obtaining the<br> session lock.<br><br>* Updated res_stasis:__stasis_app_register to release the app_registry<br> container lock before calling app_update (which locks the sesison).<br><br>ASTERISK-28423<br>Reported by Ross Beer<br><br>Change-Id: I58c69d08cb372852a63933608e4d6c3e456247b4<br>---<br>M res/ari/resource_events.c<br>M res/res_stasis.c<br>2 files changed, 38 insertions(+), 25 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/ari/resource_events.c b/res/ari/resource_events.c</span><br><span>index d935da1..f70de43 100644</span><br><span>--- a/res/ari/resource_events.c</span><br><span>+++ b/res/ari/resource_events.c</span><br><span>@@ -81,9 +81,16 @@</span><br><span> {</span><br><span> struct event_session *session = data;</span><br><span> const char *msg_type, *msg_application;</span><br><span style="color: hsl(120, 100%, 40%);">+ int app_debug_enabled;</span><br><span> </span><br><span> ast_assert(session != NULL);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * We need to get the debug flag before lockinf session</span><br><span style="color: hsl(120, 100%, 40%);">+ * to help prevent a deadlock with the apps_registry container.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ app_debug_enabled = stasis_app_get_debug_by_name(app_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ao2_lock(session);</span><br><span> </span><br><span> msg_type = S_OR(ast_json_string_get(ast_json_object_get(message, "type")), "");</span><br><span>@@ -115,7 +122,7 @@</span><br><span> msg_type,</span><br><span> msg_application);</span><br><span> } else if (stasis_app_event_allowed(app_name, message)) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (stasis_app_get_debug_by_name(app_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (app_debug_enabled) {</span><br><span> char *str = ast_json_dump_string_format(message, ast_ari_json_format());</span><br><span> </span><br><span> ast_verbose("<--- Sending ARI event to %s --->\n%s\n",</span><br><span>@@ -310,7 +317,6 @@</span><br><span> {</span><br><span> #ifdef AST_DEVMODE /* Avoid unused variable warning */</span><br><span> struct event_session *session = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> #endif</span><br><span> </span><br><span> /* event_session_shutdown should have been called before now */</span><br><span>diff --git a/res/res_stasis.c b/res/res_stasis.c</span><br><span>index 5cc758c..04b10ef 100644</span><br><span>--- a/res/res_stasis.c</span><br><span>+++ b/res/res_stasis.c</span><br><span>@@ -1704,7 +1704,7 @@</span><br><span> </span><br><span> static int __stasis_app_register(const char *app_name, stasis_app_cb handler, void *data, int all_events)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- struct stasis_app *app;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);</span><br><span> </span><br><span> if (!apps_registry) {</span><br><span> return -1;</span><br><span>@@ -1713,36 +1713,43 @@</span><br><span> ao2_lock(apps_registry);</span><br><span> app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);</span><br><span> if (app) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * We need to unlock the apps_registry before calling app_update to</span><br><span style="color: hsl(120, 100%, 40%);">+ * prevent the possibility of a deadlock with the session. We'll still</span><br><span style="color: hsl(120, 100%, 40%);">+ * run the lazy cleanup first though.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ cleanup();</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(apps_registry);</span><br><span> app_update(app, handler, data);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- app = app_create(app_name, handler, data, all_events ? STASIS_APP_SUBSCRIBE_ALL : STASIS_APP_SUBSCRIBE_MANUAL);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!app) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(apps_registry);</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%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (all_events) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct stasis_app_event_source *source;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_RWLIST_RDLOCK(&event_sources);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&event_sources, source, next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!source->subscribe) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- source->subscribe(app, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_RWLIST_UNLOCK(&event_sources);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_link_flags(apps_registry, app, OBJ_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ app = app_create(app_name, handler, data, all_events ? STASIS_APP_SUBSCRIBE_ALL : STASIS_APP_SUBSCRIBE_MANUAL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!app) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(apps_registry);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</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%);">+ if (all_events) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct stasis_app_event_source *source;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_RDLOCK(&event_sources);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&event_sources, source, next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!source->subscribe) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</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%);">+ source->subscribe(app, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_UNLOCK(&event_sources);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link_flags(apps_registry, app, OBJ_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* We lazily clean up the apps_registry, because it's good enough to</span><br><span> * prevent memory leaks, and we're lazy.</span><br><span> */</span><br><span> cleanup();</span><br><span> ao2_unlock(apps_registry);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(app, -1);</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13172">change 13172</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/+/13172"/><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: I58c69d08cb372852a63933608e4d6c3e456247b4 </div>
<div style="display:none"> Gerrit-Change-Number: 13172 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.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@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>