<p>Corey Farrell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10628">View Change</a></p><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, 28 insertions(+), 11 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/28/10628/1</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 33acb37..525cea7 100644</span><br><span>--- a/main/taskprocessor.c</span><br><span>+++ b/main/taskprocessor.c</span><br><span>@@ -769,17 +769,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>@@ -799,40 +805,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);</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/10628">change 10628</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/10628"/><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-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id64fae94a6a1e940ddf38fde622dcd4391635382 </div>
<div style="display:none"> Gerrit-Change-Number: 10628 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>