From 363293ef61b07b8ab84da6282881fc381c703d4c Mon Sep 17 00:00:00 2001 From: VivekSainiEQ Date: Fri, 8 Jan 2021 17:45:45 +0000 Subject: [PATCH] Added fix for scenario where module thread waiting for s_mutexModule in acquireGIL can deadlock with module thread waiting for s_mutex in releaseGIL Former-commit-id: 3205373bb378f895824cc1936a6bae663b1abdcc --- src/module.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/module.cpp b/src/module.cpp index 594a172e4..f128d10e3 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -5104,7 +5104,14 @@ void moduleAcquireGIL(int fServerThread) { } else { - s_mutexModule.lock(); + // It is possible that another module thread holds the GIL (and s_mutexModule as a result). + // When said thread goes to release the GIL, it will wait for s_mutex, which this thread owns. + // This thread is however waiting for the GIL (and s_mutexModule) that the other thread owns. + // As a result, a deadlock has occured. + // We release the lock on s_mutex and wait until we are able to safely acquire the GIL + // in order to prevent this deadlock from occuring. + while (!s_mutexModule.try_lock()) + s_cv.wait(lock); ++s_cAcquisitionsModule; fModuleGILWlocked++; }