<p>George Joseph <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10630">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">taskprocessor: Prevent race creating new taskprocessor.<br><br>Task processors are retrieved using a 'get or create' pattern.  The<br>singleton container was unlocked between the get and create steps so<br>it's possible that two threads could create task processors with the<br>same name at the same time.<br><br>Change-Id: Id64fae94a6a1e940ddf38fde622dcd4391635382<br>---<br>M main/taskprocessor.c<br>1 file changed, 39 insertions(+), 11 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/taskprocessor.c b/main/taskprocessor.c</span><br><span>index 7e47eaf..d475650 100644</span><br><span>--- a/main/taskprocessor.c</span><br><span>+++ b/main/taskprocessor.c</span><br><span>@@ -733,6 +733,17 @@</span><br><span>     return pvt;</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 Allocate a task processor structure</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param name Name of the task processor.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param listener Listener to associate with the task processor.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return The newly allocated task processor.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \pre tps_singletons must be locked by the caller.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> static struct ast_taskprocessor *__allocate_taskprocessor(const char *name, struct ast_taskprocessor_listener *listener)</span><br><span> {</span><br><span>        struct ast_taskprocessor *p;</span><br><span>@@ -757,17 +768,23 @@</span><br><span>         ao2_ref(p, +1);</span><br><span>      listener->tps = p;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-       if (!(ao2_link(tps_singletons, p))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ao2_link_flags(tps_singletons, p, OBJ_NOLOCK))) {</span><br><span>              ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);</span><br><span>                 listener->tps = NULL;</span><br><span>             ao2_ref(p, -2);</span><br><span>              return NULL;</span><br><span>         }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   if (p->listener->callbacks->start(p->listener)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return p;</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%);">+static struct ast_taskprocessor *__start_taskprocessor(struct ast_taskprocessor *p)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        if (p && p->listener->callbacks->start(p->listener)) {</span><br><span>           ast_log(LOG_ERROR, "Unable to start taskprocessor listener for taskprocessor %s\n",</span><br><span>                        p->name);</span><br><span>                 ast_taskprocessor_unreference(p);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>          return NULL;</span><br><span>         }</span><br><span> </span><br><span>@@ -787,40 +804,51 @@</span><br><span>                ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");</span><br><span>            return NULL;</span><br><span>         }</span><br><span style="color: hsl(0, 100%, 40%);">-       p = ao2_find(tps_singletons, name, OBJ_KEY);</span><br><span style="color: hsl(0, 100%, 40%);">-    if (p) {</span><br><span style="color: hsl(120, 100%, 40%);">+      ao2_lock(tps_singletons);</span><br><span style="color: hsl(120, 100%, 40%);">+     p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (p || (create & TPS_REF_IF_EXISTS)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          /* calling function does not want a new taskprocessor to be created if it doesn't already exist */</span><br><span style="color: hsl(120, 100%, 40%);">+                ao2_unlock(tps_singletons);</span><br><span>          return p;</span><br><span>    }</span><br><span style="color: hsl(0, 100%, 40%);">-       if (create & TPS_REF_IF_EXISTS) {</span><br><span style="color: hsl(0, 100%, 40%);">-           /* calling function does not want a new taskprocessor to be created if it doesn't already exist */</span><br><span style="color: hsl(0, 100%, 40%);">-          return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  /* Create a new taskprocessor. Start by creating a default listener */</span><br><span>       pvt = default_listener_pvt_alloc();</span><br><span>  if (!pvt) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ao2_unlock(tps_singletons);</span><br><span>          return NULL;</span><br><span>         }</span><br><span>    listener = ast_taskprocessor_listener_alloc(&default_listener_callbacks, pvt);</span><br><span>   if (!listener) {</span><br><span style="color: hsl(120, 100%, 40%);">+              ao2_unlock(tps_singletons);</span><br><span>          default_listener_pvt_destroy(pvt);</span><br><span>           return NULL;</span><br><span>         }</span><br><span> </span><br><span>        p = __allocate_taskprocessor(name, listener);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(tps_singletons);</span><br><span style="color: hsl(120, 100%, 40%);">+   p = __start_taskprocessor(p);</span><br><span>        ao2_ref(listener, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>     return p;</span><br><span> }</span><br><span> </span><br><span> struct ast_taskprocessor *ast_taskprocessor_create_with_listener(const char *name, struct ast_taskprocessor_listener *listener)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-      struct ast_taskprocessor *p = ao2_find(tps_singletons, name, OBJ_KEY);</span><br><span style="color: hsl(120, 100%, 40%);">+        struct ast_taskprocessor *p;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+      ao2_lock(tps_singletons);</span><br><span style="color: hsl(120, 100%, 40%);">+     p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);</span><br><span>    if (p) {</span><br><span style="color: hsl(120, 100%, 40%);">+              ao2_unlock(tps_singletons);</span><br><span>          ast_taskprocessor_unreference(p);</span><br><span>            return NULL;</span><br><span>         }</span><br><span style="color: hsl(0, 100%, 40%);">-       return __allocate_taskprocessor(name, listener);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    p = __allocate_taskprocessor(name, listener);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(tps_singletons);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return __start_taskprocessor(p);</span><br><span> }</span><br><span> </span><br><span> void ast_taskprocessor_set_local(struct ast_taskprocessor *tps,</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10630">change 10630</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/10630"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Id64fae94a6a1e940ddf38fde622dcd4391635382 </div>
<div style="display:none"> Gerrit-Change-Number: 10630 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Richard Mudgett <rmudgett@digium.com> </div>