Skip to content

Commit

Permalink
Merge pull request #1478 from yosefe/topic/ucs-async-fix-remove-deadlock
Browse files Browse the repository at this point in the history
UCS/ASYNC: Fix possible deadlock when removing event handler.
  • Loading branch information
yosefe authored May 6, 2017
2 parents 431d92e + a4fdc30 commit 4ffe1f2
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/ucs/async/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,6 @@ static ucs_async_handler_t *ucs_async_handler_get(int id)
return handler;
}

static ucs_async_mode_t ucs_async_handler_mode(int id)
{
ucs_async_mode_t mode;
khiter_t hash_it;

pthread_rwlock_rdlock(&ucs_async_global_context.handlers_lock);
hash_it = ucs_async_handler_kh_get(id);
if (ucs_async_handler_kh_is_end(hash_it)) {
mode = UCS_ASYNC_MODE_POLL;
} else {
mode = kh_value(&ucs_async_global_context.handlers, hash_it)->mode;
}
pthread_rwlock_unlock(&ucs_async_global_context.handlers_lock);
return mode;
}

/* remove from hash and return the handler */
static ucs_async_handler_t *ucs_async_handler_extract(int id)
{
Expand Down Expand Up @@ -477,13 +461,16 @@ ucs_status_t ucs_async_add_timer(ucs_async_mode_t mode, ucs_time_t interval,

ucs_status_t ucs_async_remove_handler(int id, int sync)
{
ucs_async_mode_t mode = ucs_async_handler_mode(id);
ucs_async_handler_t *handler;
ucs_status_t status;

ucs_async_method_call(mode, block);
/* We can't find the async handle mode without taking a read lock, which in
* turn may cause a deadlock if async handle is running. So we have to block
* all modes.
*/
ucs_async_method_call_all(block);
handler = ucs_async_handler_extract(id);
ucs_async_method_call(mode, unblock);
ucs_async_method_call_all(unblock);
if (handler == NULL) {
return UCS_ERR_NO_ELEM;
}
Expand Down

0 comments on commit 4ffe1f2

Please sign in to comment.