From dbd2ef2d8b461c72aa26473be32f6de9c1236c31 Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Tue, 4 Apr 2017 12:47:14 -0600 Subject: [PATCH] FS-10167: Fixed sessions to cleanup external from their own state processing thread to avoid deadlocking on thread join during cleanup. --- libs/libblade/src/blade_session.c | 3 +-- libs/libblade/src/blade_stack.c | 17 +++++++++++++++++ libs/libblade/src/include/blade_types.h | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libs/libblade/src/blade_session.c b/libs/libblade/src/blade_session.c index f284d90045..3d0c61c17d 100644 --- a/libs/libblade/src/blade_session.c +++ b/libs/libblade/src/blade_session.c @@ -505,8 +505,7 @@ ks_status_t blade_session_state_on_destroy(blade_session_t *bs) ks_log(KS_LOG_DEBUG, "Session (%s) state destroy\n", bs->id); - blade_handle_sessions_remove(bs); - blade_session_destroy(&bs); + blade_session_state_set(bs, BLADE_SESSION_STATE_CLEANUP); // @todo ignoring returns for now, see what makes sense later return KS_STATUS_SUCCESS; diff --git a/libs/libblade/src/blade_stack.c b/libs/libblade/src/blade_stack.c index 9af67c4a22..5a8a81b800 100644 --- a/libs/libblade/src/blade_stack.c +++ b/libs/libblade/src/blade_stack.c @@ -889,6 +889,7 @@ void *blade_handle_worker_thread(ks_thread_t *thread, void *data) { blade_handle_t *bh = NULL; blade_connection_t *bc = NULL; + blade_session_t *bs = NULL; ks_hash_iterator_t *it = NULL; ks_q_t *cleanup = NULL; @@ -917,6 +918,22 @@ void *blade_handle_worker_thread(ks_thread_t *thread, void *data) blade_connection_destroy(&bc); } + ks_hash_write_lock(bh->sessions); + for (it = ks_hash_first(bh->sessions, KS_UNLOCKED); it; it = ks_hash_next(&it)) { + void *key = NULL; + blade_session_t *value = NULL; + + ks_hash_this(it, (const void **)&key, NULL, (void **)&value); + + if (blade_session_state_get(value) == BLADE_SESSION_STATE_CLEANUP) ks_q_push(cleanup, value); + } + ks_hash_write_unlock(bh->sessions); + + while (ks_q_trypop(cleanup, (void **)&bs) == KS_STATUS_SUCCESS) { + blade_handle_sessions_remove(bs); + blade_session_destroy(&bs); + } + ks_sleep_ms(500); } diff --git a/libs/libblade/src/include/blade_types.h b/libs/libblade/src/include/blade_types.h index 71326f0b3a..6bd23134e5 100644 --- a/libs/libblade/src/include/blade_types.h +++ b/libs/libblade/src/include/blade_types.h @@ -105,6 +105,7 @@ typedef enum { typedef enum { BLADE_SESSION_STATE_NONE, + BLADE_SESSION_STATE_CLEANUP, BLADE_SESSION_STATE_DESTROY, BLADE_SESSION_STATE_HANGUP, BLADE_SESSION_STATE_CONNECT,