diff --git a/channels/chan_agent.c b/channels/chan_agent.c
index 466181cb6f08aceae7df7b4394e1723fe371481c..b3ae6f0b892c7e353c977214fb8b23f64ae18cef 100755
--- a/channels/chan_agent.c
+++ b/channels/chan_agent.c
@@ -560,6 +560,8 @@ static struct ast_channel *agent_new(struct agent_pvt *p, int state)
 				tmp->pvt->pvt = NULL;
 				p->app_sleep_cond = 1;
 				ast_channel_free( tmp );
+				ast_pthread_mutex_unlock(&p->lock);	/* For other thread to read the condition. */
+				ast_pthread_mutex_unlock(&p->app_lock);
 				return NULL;
 			}
 		}
diff --git a/channels/chan_local.c b/channels/chan_local.c
index 7567c81e49611919feace839a3814263144e3ef2..7267a8d1ff26cbb012d0bf0377ad16fec9db1166 100755
--- a/channels/chan_local.c
+++ b/channels/chan_local.c
@@ -71,11 +71,14 @@ static struct local_pvt {
 
 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f)
 {
-	struct ast_channel *other;
-	if (isoutbound)
+	struct ast_channel *other, *us;
+	if (isoutbound) {
+		us = p->chan;
 		other = p->owner;
-	else
+	} else {
+		us = p->owner;
 		other = p->chan;
+	}
 	if (!other)
 		return 0;
 	p->glaredetect = 1;
@@ -90,8 +93,11 @@ retrylock:
 	if (pthread_mutex_trylock(&other->lock)) {
 		/* Failed to lock.  Release main lock and try again */
 		ast_pthread_mutex_unlock(&p->lock);
+		ast_pthread_mutex_unlock(&us->lock);
 		/* Wait just a bit */
 		usleep(1);
+		/* Only we can destroy ourselves, so we can't disappear here */
+		ast_pthread_mutex_lock(&us->lock);
 		ast_pthread_mutex_lock(&p->lock);
 		goto retrylock;
 	}