Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 103725) +++ channels/chan_sip.c (working copy) @@ -3064,6 +3064,21 @@ ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid); } + /* Unlink us from the owner if we have one */ + if (p->owner) { + if (lockowner) + ast_channel_lock(p->owner); + if (option_debug) + ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name); + p->owner->tech_pvt = NULL; + /* Make sure that the channel knows its backend is going away */ + p->owner->_softhangup |= AST_SOFTHANGUP_DEV; + if (lockowner) + ast_channel_unlock(p->owner); + /* Give the channel a chance to react before deallocation */ + usleep(1); + } + /* Remove link from peer to subscription of MWI */ if (p->relatedpeer && p->relatedpeer->mwipvt) p->relatedpeer->mwipvt = NULL; @@ -3080,6 +3095,10 @@ AST_SCHED_DEL(sched, p->waitid); AST_SCHED_DEL(sched, p->autokillid); + /* We absolutely cannot destroy the rtp struct while a bridge is active or we WILL crash */ + while (ast_rtp_get_bridged(p->rtp)) + usleep(1); + if (p->rtp) ast_rtp_destroy(p->rtp); if (p->vrtp) @@ -3098,16 +3117,6 @@ ASTOBJ_UNREF(p->registry, sip_registry_destroy); } - /* Unlink us from the owner if we have one */ - if (p->owner) { - if (lockowner) - ast_channel_lock(p->owner); - if (option_debug) - ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name); - p->owner->tech_pvt = NULL; - if (lockowner) - ast_channel_unlock(p->owner); - } /* Clear history */ if (p->history) { struct sip_history *hist; Index: main/rtp.c =================================================================== --- main/rtp.c (revision 103725) +++ main/rtp.c (working copy) @@ -3266,6 +3266,14 @@ ast_channel_lock(c0); } + /* Ensure neither channel got hungup during lock avoidance */ + if (ast_check_hangup(c0) || ast_check_hangup(c1)) { + ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name); + ast_channel_unlock(c0); + ast_channel_unlock(c1); + return AST_BRIDGE_FAILED; + } + /* Find channel driver interfaces */ if (!(pr0 = get_proto(c0))) { ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);