From ca141eaf1c01a7ee88ac9491fdafa40de5c057b4 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Wed, 24 May 2023 13:37:34 -0700 Subject: [PATCH 1/2] Release the GIL before any call to async methods --- src/utils.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils.cc b/src/utils.cc index 8ebc3f9..f45b801 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -21,7 +21,16 @@ void waitForAsyncResult(std::function func) { auto promise = std::make_shared>(); - func([promise](Result result) { promise->set_value(result); }); + + { + // Always call the Pulsar C++ client methods without holding + // the GIL. This avoids deadlocks due the sequence of acquiring + // mutexes by different threads. eg: + // Thread-1: GIL -> producer.lock + // Thread-2: producer.lock -> GIL (In a callback) + py::gil_scoped_release release; + func([promise](Result result) { promise->set_value(result); }); + } internal::waitForResult(*promise); } From 336d7797440fba956260ddf98e1e3be1d427b87b Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Wed, 24 May 2023 14:02:50 -0700 Subject: [PATCH 2/2] Also on waitForAsyncValue() --- src/utils.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils.h b/src/utils.h index bbe202e..910f0cd 100644 --- a/src/utils.h +++ b/src/utils.h @@ -49,10 +49,14 @@ inline T waitForAsyncValue(std::function>(); auto valuePromise = std::make_shared>(); - func([resultPromise, valuePromise](Result result, const T& value) { - valuePromise->set_value(value); - resultPromise->set_value(result); - }); + { + py::gil_scoped_release release; + + func([resultPromise, valuePromise](Result result, const T& value) { + valuePromise->set_value(value); + resultPromise->set_value(result); + }); + } internal::waitForResult(*resultPromise); return valuePromise->get_future().get();