Index: main/channel.c =================================================================== --- main/channel.c (revision 287268) +++ main/channel.c (working copy) @@ -5566,19 +5566,27 @@ ast_debug(1, "Planning to masquerade channel %s into the structure of %s\n", clonechan->name, original->name); - if (original->masq) { - ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n", - original->masq->name, original->name); - } else if (clonechan->masqr) { - ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n", - clonechan->name, clonechan->masqr->name); - } else { + + if (!original->masqr && !original->masq && !clonechan->masq && !clonechan->masqr) { original->masq = clonechan; clonechan->masqr = original; ast_queue_frame(original, &ast_null_frame); ast_queue_frame(clonechan, &ast_null_frame); ast_debug(1, "Done planning to masquerade channel %s into the structure of %s\n", clonechan->name, original->name); res = 0; + } else if (original->masqr) { + /* not yet as a previously planned masq hasn't yet happened */ + ast_log(LOG_WARNING, "%s (1) is already going to masquerade as %s\n", + original->name, original->masqr->name); + } else if (original->masq) { + ast_log(LOG_WARNING, "%s (2) is already going to masquerade as %s\n", + original->masq->name, original->name); + } else if (clonechan->masq) { + ast_log(LOG_WARNING, "%s (3) is already going to masquerade as %s\n", + clonechan->masq->name, clonechan->name); + } else { /* (clonechan->masqr) */ + ast_log(LOG_WARNING, "%s (4) is already going to masquerade as %s\n", + clonechan->name, clonechan->masqr->name); } ast_channel_unlock(clonechan); @@ -5877,8 +5885,8 @@ struct ast_channel *clonechan, *chans[2]; struct ast_channel *bridged; struct ast_cdr *cdr; - format_t rformat = original->readformat; - format_t wformat = original->writeformat; + format_t rformat; + format_t wformat; char newn[AST_CHANNEL_NAME]; char orig[AST_CHANNEL_NAME]; char masqn[AST_CHANNEL_NAME]; @@ -5925,6 +5933,10 @@ CHANNEL_DEADLOCK_AVOIDANCE(original); } + /* remember the read and write formats */ + rformat = original->readformat; + wformat = original->writeformat; + /* clear the masquerade channels */ original->masq = NULL; clonechan->masqr = NULL; @@ -5932,10 +5944,10 @@ /* unlink from channels container as name (which is the hash value) will change */ ao2_unlink(channels, original); ao2_unlink(channels, clonechan); - +#if 0 /* now that both channels are locked and unlinked from the container, it is safe to unlock it */ ao2_unlock(channels); - +#endif ast_debug(4, "Actually Masquerading %s(%d) into the structure of %s(%d)\n", clonechan->name, clonechan->_state, original->name, original->_state); @@ -6218,6 +6230,7 @@ ao2_link(channels, original); } + ao2_unlock(channels); return 0; }