Merged revisions 128795 via svnmerge from
[asterisk/asterisk.git] / channels / chan_iax2.c
index 831d6f6..1a749b6 100644 (file)
@@ -4091,11 +4091,13 @@ static struct ast_channel *ast_iax2_new(int callno, int state, int capability)
        ast_mutex_unlock(&iaxsl[callno]);
        tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "IAX2/%s-%d", i->host, i->callno);
        ast_mutex_lock(&iaxsl[callno]);
-       if (!iaxs[callno]) {
+       if (i != iaxs[callno]) {
                if (tmp) {
+                       /* unlock and relock iaxsl[callno] to preserve locking order */
+                       ast_mutex_unlock(&iaxsl[callno]);
                        ast_channel_free(tmp);
+                       ast_mutex_lock(&iaxsl[callno]);
                }
-               ast_mutex_unlock(&iaxsl[callno]);
                return NULL;
        }
        iax2_ami_channelupdate(i);