From 3bfcbd2cb77897884e1c3cd60df12310f9b35145 Mon Sep 17 00:00:00 2001 From: wangguibao Date: Wed, 24 Apr 2019 15:56:34 +0800 Subject: [PATCH] Fix dead-lock condition with PaddlePaddle GPU predictor --- predictor/src/pdserving.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/predictor/src/pdserving.cpp b/predictor/src/pdserving.cpp index 20e32a99..0897039c 100644 --- a/predictor/src/pdserving.cpp +++ b/predictor/src/pdserving.cpp @@ -70,13 +70,26 @@ DEFINE_bool(g, false, "user defined gflag path"); DECLARE_string(flagfile); namespace bthread { -extern pthread_mutex_t g_task_control_mutex; + extern pthread_mutex_t g_task_control_mutex; } +pthread_mutex_t g_worker_start_fn_mutex = PTHREAD_MUTEX_INITIALIZER; void pthread_worker_start_fn() { - while (pthread_mutex_lock(&bthread::g_task_control_mutex) != 0) {} + while (pthread_mutex_lock(&g_worker_start_fn_mutex) != 0) {} + + // Try to avoid deadlock in bthread + int lock_status = pthread_mutex_trylock(&bthread::g_task_control_mutex); + if (lock_status == EBUSY || lock_status == EAGAIN) { + pthread_mutex_unlock(&bthread::g_task_control_mutex); + } Resource::instance().thread_initialize(); - pthread_mutex_unlock(&bthread::g_task_control_mutex); + + // Try to avoid deadlock in bthread + if (lock_status == EBUSY || lock_status == EAGAIN) { + while (pthread_mutex_lock(&bthread::g_task_control_mutex) != 0) {} + } + + pthread_mutex_unlock(&g_worker_start_fn_mutex); } static void g_change_server_port() { -- GitLab