From c05ebd85e16aceaff115865048cd551864b95821 Mon Sep 17 00:00:00 2001 From: Santiago De la Cruz Date: Wed, 6 Dec 2023 15:24:05 -0400 Subject: [PATCH] pjsua: fix deadlock setting null sound device Also, use PJSUA_RELEASE_LOCK and PJSUA_RELOCK when needed --- pjsip/src/pjsua-lib/pjsua_aud.c | 18 +++++++++++++++--- pjsip/src/pjsua-lib/pjsua_call.c | 8 ++------ pjsip/src/pjsua-lib/pjsua_core.c | 5 +---- pjsip/src/pjsua-lib/pjsua_vid.c | 8 ++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pjsip/src/pjsua-lib/pjsua_aud.c b/pjsip/src/pjsua-lib/pjsua_aud.c index bc935b5611..5ee718dad9 100644 --- a/pjsip/src/pjsua-lib/pjsua_aud.c +++ b/pjsip/src/pjsua-lib/pjsua_aud.c @@ -2409,8 +2409,14 @@ PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void) /* Create memory pool for sound device. */ pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); - PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); - + if (!pjsua_var.snd_pool) { + pjsua_perror(THIS_FILE, "Unable to create pool for null sound device", + PJ_ENOMEM); + PJSUA_UNLOCK(); + pj_log_pop_indent(); + return PJ_ENOMEM; + } + PJ_LOG(4,(THIS_FILE, "Opening null sound device..")); /* Get the port0 of the conference bridge. */ @@ -2432,7 +2438,13 @@ PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void) /* Start the master port */ status = pjmedia_master_port_start(pjsua_var.null_snd); - PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to start null sound device", + status); + PJSUA_UNLOCK(); + pj_log_pop_indent(); + return status; + } pjsua_var.no_snd = PJ_FALSE; pjsua_var.snd_is_on = PJ_TRUE; diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c index 0a78d0f963..c1dd6cfcf2 100644 --- a/pjsip/src/pjsua-lib/pjsua_call.c +++ b/pjsip/src/pjsua-lib/pjsua_call.c @@ -5117,10 +5117,7 @@ static void pjsua_call_on_state_changed(pjsip_inv_session *inv, } /* Release locks before calling callbacks, to avoid deadlock. */ - while (PJSUA_LOCK_IS_LOCKED()) { - num_locks++; - PJSUA_UNLOCK(); - } + num_locks = PJSUA_RELEASE_LOCK(); /* Ticket #1627: Invoke on_call_tsx_state() when call is disconnected. * @@ -5142,8 +5139,7 @@ static void pjsua_call_on_state_changed(pjsip_inv_session *inv, (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e); /* Re-acquire the locks. */ - for (;num_locks > 0; num_locks--) - PJSUA_LOCK(); + PJSUA_RELOCK(num_locks); /* call->inv may be NULL now */ diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index 943368021d..23f0b70209 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -3850,10 +3850,7 @@ static pj_status_t restart_listener(pjsua_transport_id id, unsigned num_locks = 0; /* Release locks before restarting the transport, to avoid deadlock. */ - while (PJSUA_LOCK_IS_LOCKED()) { - num_locks++; - PJSUA_UNLOCK(); - } + num_locks = PJSUA_RELEASE_LOCK(); status = pjsip_udp_transport_restart2( pjsua_var.tpdata[id].data.tp, diff --git a/pjsip/src/pjsua-lib/pjsua_vid.c b/pjsip/src/pjsua-lib/pjsua_vid.c index d7f701d492..c34001d13f 100644 --- a/pjsip/src/pjsua-lib/pjsua_vid.c +++ b/pjsip/src/pjsua-lib/pjsua_vid.c @@ -1349,10 +1349,7 @@ void pjsua_vid_stop_stream(pjsua_call_media *call_med) pjmedia_vid_stream_send_rtcp_bye(strm); /* Release locks before unsubscribing, to avoid deadlock. */ - while (PJSUA_LOCK_IS_LOCKED()) { - num_locks++; - PJSUA_UNLOCK(); - } + num_locks = PJSUA_RELEASE_LOCK(); /* Unsubscribe events first, otherwise the event callbacks * can be called and access already destroyed objects. @@ -1388,8 +1385,7 @@ void pjsua_vid_stop_stream(pjsua_call_media *call_med) pjmedia_event_unsubscribe(NULL, &call_media_on_event, call_med, strm); /* Re-acquire the locks. */ - for (; num_locks > 0; num_locks--) - PJSUA_LOCK(); + PJSUA_RELOCK(num_locks); PJSUA_LOCK();