Skip to content

Commit 1bf2b2b

Browse files
committed
fix(Mutex_POSIX): prevent deadlocks when logging inside signal handlers
Fixes #4939.
1 parent ee4f89f commit 1bf2b2b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ option(POCO_ENABLE_STD_MUTEX "Set to OFF|NO using mutex from standard library (d
247247
if (POCO_ENABLE_STD_MUTEX)
248248
add_definitions(-DPOCO_ENABLE_STD_MUTEX)
249249
endif ()
250+
251+
option(POCO_ENABLE_SIG_LOCK "Set to ON if signal handlers use code that lock mutexes to prevent deadlocks (default OFF)" OFF)
252+
253+
if (POCO_ENABLE_SIG_LOCK)
254+
add_definitions(-DPOCO_ENABLE_SIG_LOCK)
255+
endif ()
250256
include(DefinePlatformSpecifc)
251257

252258
# Collect the built libraries and include dirs, the will be used to create the PocoConfig.cmake file

Foundation/include/Poco/Mutex_POSIX.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <pthread.h>
2424
#include <errno.h>
2525

26+
#ifdef POCO_ENABLE_SIG_LOCK
27+
#include <signal.h>
28+
#endif
29+
2630

2731
namespace Poco {
2832

@@ -40,6 +44,7 @@ class Foundation_API MutexImpl
4044

4145
private:
4246
pthread_mutex_t _mutex;
47+
sigset_t set, oldset;
4348
};
4449

4550

@@ -56,6 +61,10 @@ class Foundation_API FastMutexImpl: public MutexImpl
5661
//
5762
inline void MutexImpl::lockImpl()
5863
{
64+
#ifdef POCO_ENABLE_SIG_LOCK
65+
sigfillset(&set);
66+
pthread_sigmask(SIG_BLOCK, &set, &oldset);
67+
#endif
5968
if (pthread_mutex_lock(&_mutex))
6069
throw SystemException("cannot lock mutex");
6170
}
@@ -77,6 +86,10 @@ inline void MutexImpl::unlockImpl()
7786
{
7887
if (pthread_mutex_unlock(&_mutex))
7988
throw SystemException("cannot unlock mutex");
89+
90+
#ifdef POCO_ENABLE_SIG_LOCK
91+
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
92+
#endif
8093
}
8194

8295

0 commit comments

Comments
 (0)