Merge pull request #1727 from signalwire/session_read_lock

[Core] Fix possible race condition between locking a session and reading it's flags in switch_core_session_read_lock() and switch_core_session_read_lock_hangup().
This commit is contained in:
Andrey Volk 2022-07-26 02:28:50 +03:00 committed by GitHub
commit 6bed9e163f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 21 deletions

View File

@ -85,9 +85,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock(switch_core_sessio
switch_status_t status = SWITCH_STATUS_FALSE;
if (session->rwlock) {
if ((status = switch_thread_rwlock_tryrdlock(session->rwlock)) == SWITCH_STATUS_SUCCESS) {
if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_down_nosig(session->channel)) {
status = SWITCH_STATUS_FALSE;
if (switch_thread_rwlock_tryrdlock(session->rwlock) == SWITCH_STATUS_SUCCESS) {
if (switch_channel_test_flag(session->channel, CF_THREAD_SLEEPING)) {
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Ping thread\n",
@ -95,20 +95,20 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock(switch_core_sessio
#endif
switch_core_session_wake_session_thread(session);
}
switch_thread_rwlock_unlock(session->rwlock);
}
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock FAIL\n",
switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
#endif
switch_thread_rwlock_unlock(session->rwlock);
} else {
status = (switch_status_t) switch_thread_rwlock_tryrdlock(session->rwlock);
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock ACQUIRED\n",
switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
#endif
}
}
}
return status;
}
@ -123,20 +123,22 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock_hangup(switch_core
switch_status_t status = SWITCH_STATUS_FALSE;
if (session->rwlock) {
if ((status = switch_thread_rwlock_tryrdlock(session->rwlock)) == SWITCH_STATUS_SUCCESS) {
if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_get_state(session->channel) >= CS_DESTROY) {
status = SWITCH_STATUS_FALSE;
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock FAIL\n",
switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
#endif
switch_thread_rwlock_unlock(session->rwlock);
} else {
status = (switch_status_t) switch_thread_rwlock_tryrdlock(session->rwlock);
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock ACQUIRED\n",
switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
#endif
}
}
}
return status;
}