New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static FastMutex fails to lock when issued from another thread on linux #3918
Comments
thank you @JoKre17 |
Thanks for fixing this particular occurrence of the issue regarding Poco::Mutex. Here is a quick grep to find some more occurrences which don't even include cases where a Mutex/FastMutex is a member of another static object:
As described in this issue I was not able to exactly identify the cause of the bug. In our case I replaced all usages of Mutex and FastMutex with std::mutex which then behave as expected. |
I think that we can use Poco::Mutex and Poco::FastMutex as wrappers for std::recursive_mutex and std::mutex instead of replacing For using std::*mutexes switch on cmake-option POCO_ENABLE_STD_MUTEX
* Complimentary to #3918 I think that we can use Poco::Mutex and Poco::FastMutex as wrappers for std::recursive_mutex and std::mutex instead of replacing For using std::*mutexes switch on cmake-option POCO_ENABLE_STD_MUTEX * add define POCO_ENABLE_STD_MUTEX to the Config.h remove empty if-else from CMakeLists.txt
* Complimentary to #3918 I think that we can use Poco::Mutex and Poco::FastMutex as wrappers for std::recursive_mutex and std::mutex instead of replacing For using std::*mutexes switch on cmake-option POCO_ENABLE_STD_MUTEX * add define POCO_ENABLE_STD_MUTEX to the Config.h remove empty if-else from CMakeLists.txt
Hello,
as the title says, we are getting an error and undefined behavior after logging 2-3 messages when using the LoggingConfigurator with the following logging properties file:
We are using the AsyncChannel which runs another Thread. Further down the logging channel chain we are reaching the channel named "console" which is wrapped with the formatter named "pretty" which is calling
PatternFormatter::format
.As we include
"[%z]"
in the format, the function in return callsTimezone::utcOffset()
.Under linux the Timezone implementation contains the following (I've stripped out the unrelevant parts):
As
TZInfo tzInfo
including the FastMutex member is static, it will be created by the main thread. Now the logging contains an AsyncChannel running on a different thread which then fails to lock the FastMutex. (Line 27 inFoundation/src/Timezone_UNIX.cpp
)The code running
pthread_mutex_lock
(in this caseMutexImpl::lockImpl
) throws an exception without any catch handler which is likely the cause for the process hanging.It seems on
pthread_mutex_lock(&_mutex)
we are getting the error code for EINVAL indicating that our logging thread has a higher priority than the main thread or the mutex handle_mutex
is invalid.Linux documentation says:
The latter error is unlikely as we checked the mutex lock/unlock after initialization and dumped the
pthread_mutex_t
object showing no modification between construction and later after the error bylockImpl
.After debugging it seems weird to get this error as per default Poco does not set the protocol attribute (therefore it's
PTHREAD_PRIO_NONE
) and Poco seems to only creates threads with the schedule policySCHED_OTHER
(meaning no real time threads) and thus the priority must be 0.After all of this I checked the implementation of
tzset()
to see if it's not already thread safely implemented.And indeed, as discussed on Stack Overflow here, the tzset function already uses a lock as one can see here (glib.c) in line 562.
Therefore potential fixes for the issues with TZInfo in Poco could be solved by either removing the usage of
Poco::FastMutex
or to switch to a native mutex from stdlib or to use aPoco::SpinlockMutex
.Here is a minimal example to reproduce the bug:
The following screenshot with two example executions shows the undefined behavior.
The text was updated successfully, but these errors were encountered: