Skip to content

Commit

Permalink
[core, mod_sofia] Fix codec set deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkarolczyk authored Oct 11, 2024
1 parent a891067 commit b4ebd09
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/include/switch_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,17 @@ SWITCH_DECLARE(void) switch_core_session_unlock_codec_write(_In_ switch_core_ses
SWITCH_DECLARE(void) switch_core_session_lock_codec_read(_In_ switch_core_session_t *session);
SWITCH_DECLARE(void) switch_core_session_unlock_codec_read(_In_ switch_core_session_t *session);

/*!
\brief Lock codec read mutex and codec write mutex using trylock in an infinite loop
\param session session to lock the codec in
*/
SWITCH_DECLARE(void) switch_core_codec_lock_full(switch_core_session_t *session);

/*!
\brief Unlock codec read mutex and codec write mutex
\param session session to unlock the codec in
*/
SWITCH_DECLARE(void) switch_core_codec_unlock_full(switch_core_session_t *session);

SWITCH_DECLARE(switch_status_t) switch_core_session_get_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp);
SWITCH_DECLARE(switch_status_t) switch_core_session_get_real_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp);
Expand Down
13 changes: 12 additions & 1 deletion src/mod/endpoints/mod_sofia/mod_sofia.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,15 +1339,17 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi

if (msg->message_id == SWITCH_MESSAGE_INDICATE_SIGNAL_DATA) {
sofia_dispatch_event_t *de = (sofia_dispatch_event_t *) msg->pointer_arg;

switch_core_session_lock_codec_write(session);
switch_mutex_lock(tech_pvt->sofia_mutex);
if (switch_core_session_in_thread(session)) {
de->session = session;
}

sofia_process_dispatch_event(&de);


switch_mutex_unlock(tech_pvt->sofia_mutex);
switch_core_session_unlock_codec_write(session);
goto end;
}

Expand All @@ -1363,6 +1365,9 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi

/* ones that do not need to lock sofia mutex */
switch (msg->message_id) {
case SWITCH_MESSAGE_INDICATE_TRANSCODING_NECESSARY:
case SWITCH_MESSAGE_RESAMPLE_EVENT:
goto end;
case SWITCH_MESSAGE_INDICATE_KEEPALIVE:
{
if (msg->numeric_arg) {
Expand Down Expand Up @@ -1523,6 +1528,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
}

/* ones that do need to lock sofia mutex */
switch_core_session_lock_codec_write(session);
switch_mutex_lock(tech_pvt->sofia_mutex);

if (switch_channel_down(channel) || sofia_test_flag(tech_pvt, TFLAG_BYE)) {
Expand Down Expand Up @@ -1557,7 +1563,9 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
tech_pvt->proxy_refer_uuid = (char *)msg->string_array_arg[0];
} else if (!switch_channel_var_true(tech_pvt->channel, "sip_refer_continue_after_reply")) {
switch_mutex_unlock(tech_pvt->sofia_mutex);
switch_core_session_unlock_codec_write(session);
sofia_wait_for_reply(tech_pvt, 9999, 10);
switch_core_session_lock_codec_write(session);
switch_mutex_lock(tech_pvt->sofia_mutex);

if ((var = switch_channel_get_variable(tech_pvt->channel, "sip_refer_reply"))) {
Expand Down Expand Up @@ -2391,13 +2399,15 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
/* Unlock the session signal to allow the ack to make it in */
// Maybe we should timeout?
switch_mutex_unlock(tech_pvt->sofia_mutex);
switch_core_session_unlock_codec_write(session);

while (switch_channel_ready(channel) && !sofia_test_flag(tech_pvt, TFLAG_3PCC_HAS_ACK)) {
switch_ivr_parse_all_events(session);
switch_yield(10000);
}

/* Regain lock on sofia */
switch_core_session_lock_codec_write(session);
switch_mutex_lock(tech_pvt->sofia_mutex);

switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "3PCC-PROXY, Done waiting for ACK\n");
Expand Down Expand Up @@ -2706,6 +2716,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
//}

switch_mutex_unlock(tech_pvt->sofia_mutex);
switch_core_session_unlock_codec_write(session);

end:

Expand Down
21 changes: 21 additions & 0 deletions src/switch_core_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ SWITCH_DECLARE(void) switch_core_session_unset_read_codec(switch_core_session_t
switch_mutex_unlock(session->codec_read_mutex);
}

SWITCH_DECLARE(void) switch_core_codec_lock_full(switch_core_session_t *session)
{
do {
if (switch_mutex_trylock(session->codec_write_mutex) == SWITCH_STATUS_SUCCESS) {
if (switch_mutex_trylock(session->codec_read_mutex) == SWITCH_STATUS_SUCCESS) {
return;
}

switch_mutex_unlock(session->codec_write_mutex);
}

switch_cond_next();
} while (1);
}

SWITCH_DECLARE(void) switch_core_codec_unlock_full(switch_core_session_t *session)
{
switch_mutex_unlock(session->codec_read_mutex);
switch_mutex_unlock(session->codec_write_mutex);
}

SWITCH_DECLARE(void) switch_core_session_lock_codec_write(switch_core_session_t *session)
{
switch_mutex_lock(session->codec_write_mutex);
Expand Down
6 changes: 2 additions & 4 deletions src/switch_core_media.c
Original file line number Diff line number Diff line change
Expand Up @@ -3601,8 +3601,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_set_codec(switch_core_session_

switch_assert(session);

switch_core_session_lock_codec_write(session);
switch_core_session_lock_codec_read(session);
switch_core_codec_lock_full(session);

switch_mutex_lock(session->codec_init_mutex);

Expand Down Expand Up @@ -3756,8 +3755,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_set_codec(switch_core_session_

switch_mutex_unlock(session->codec_init_mutex);

switch_core_session_unlock_codec_read(session);
switch_core_session_unlock_codec_write(session);
switch_core_codec_unlock_full(session);

return status;
}
Expand Down
4 changes: 4 additions & 0 deletions src/switch_core_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,8 @@ SWITCH_DECLARE(void) switch_core_session_reset(switch_core_session_t *session, s
{
switch_channel_t *channel = switch_core_session_get_channel(session);

switch_core_codec_lock_full(session);

if (reset_read_codec) {
switch_core_session_set_read_codec(session, NULL);
if (session->sdata && switch_core_codec_ready(&session->sdata->codec)) {
Expand Down Expand Up @@ -1425,6 +1427,8 @@ SWITCH_DECLARE(void) switch_core_session_reset(switch_core_session_t *session, s
switch_clear_flag(session, SSF_WARN_TRANSCODE);
switch_ivr_deactivate_unicast(session);
switch_channel_clear_flag(channel, CF_BREAK);

switch_core_codec_unlock_full(session);
}


Expand Down

0 comments on commit b4ebd09

Please sign in to comment.