Skip to content

Commit

Permalink
pjsua: fix deadlock setting null sound device (#3799)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhit authored Dec 11, 2023
1 parent 78b73c9 commit 648aa3c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
18 changes: 15 additions & 3 deletions pjsip/src/pjsua-lib/pjsua_aud.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions pjsip/src/pjsua-lib/pjsua_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 */

Expand Down
5 changes: 1 addition & 4 deletions pjsip/src/pjsua-lib/pjsua_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions pjsip/src/pjsua-lib/pjsua_vid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 648aa3c

Please sign in to comment.