This PR fixes issue #569 by using a mutex in the begin_event
, end_event
functions.
Since I could not find anywhere which C++ version is the minimum supported one, I wrote a custom guard class based on pthread instead of using the one provided with C++11.
The fix was used in a large TTCN project (> 10.000 LOC TTCN) and resolved the issue.
The current implementation of the LoggerPluginManager is not thread safe and therefore logging from multiple threads at the same time might cause double frees and access after free errors.
Create a hello world TITAN project using Eclipse and insert the following code:
Makefile:
...
# Flags for the C++ compiler:
CXXFLAGS = -Wall -O0 -g -pthread
# Flags for the linker:
LDFLAGS = -pthread
...
PCOType.cc:
...
#include <pthread.h>
...
void* thread_main(void* arg) {
for (int i = 0; i < 10000; ++i) {
TTCN_Logger::begin_event(TTCN_USER);
TTCN_Logger::log_event("Side thread: Message");
TTCN_Logger::end_event();
}
return NULL;
}
...
void PCOType::user_map(const char *system_port)
{
...
pthread_t thread;
pthread_create(&thread, NULL, thread_main, NULL);
for (int i = 0; i < 10000; ++i) {
TTCN_Logger::begin_event(TTCN_USER);
TTCN_Logger::log_event("Main thread: Message");
TTCN_Logger::end_event();
}
}
The HTC crashes with a segmentation fault.
Log output is written without memory errors.
Execution log HTC:
TTCN-3 Host Controller (parallel mode), version 7/CAX 105 7730 R2A
./LogThreadingProblem: Segmentation fault occurred
./LogThreadingProblem(+0xd5bd7)[0x56033c942bd7]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x153c0)[0x7fc5352d63c0]
./LogThreadingProblem(+0x36af0a)[0x56033cbd7f0a]
./LogThreadingProblem(+0x624c7)[0x56033c8cf4c7]
./LogThreadingProblem(+0x589c5)[0x56033c8c59c5]
./LogThreadingProblem(+0x2889b)[0x56033c89589b]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x9609)[0x7fc5352ca609]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x43)[0x7fc534ea3293]
We have already fixed the problem with a mutex around the begin_event and end_event functions. I'll provide a fixed version on my fork and add the link as a comment here. Just opening this issue in case someone else has a better solution.
7.2.0 and 8.0.0
Ubuntu 20.04
/cc @aknappqwt
Like you said I'm now using a POSIX pipe to transfer data between the threads and so far it seems to work fine. So I'll close the corresponding PR and this issue. Thanks for your suggestion!
I guess you were using simple POSIX pipes or are there already wrapper classes for this kind of problem? In this case I'll try to rewrite our problematic test port to use your proposed solution.
I just wonder why it all worked once I simply added the locks as shown in the merge request.
Ah I wasn't aware of that. However, even if I have a background thread sending the data via pipes, it would still be nice to have at least logging available to track down potential errors.
This PR fixes issue #569 by using a mutex in the begin_event
, end_event
functions.
Since I could not find anywhere which C++ version is the minimum supported one, I wrote a custom guard class based on pthread instead of using the one provided with C++11.
The fix was used in a large TTCN project (> 10.000 LOC TTCN) and resolved the issue.
Tobias Erbshäußer (ITK Engineering GmbH) (0e641c80) at 09 Aug 13:11
Guarded log functions with mutex to prevent threading errors (issue...
Tobias Erbshäußer (ITK Engineering GmbH) (aa1d70bf) at 09 Aug 13:03
Guarded log functions with mutex to prevent threading errors.
The current implementation of the LoggerPluginManager is not thread safe and therefore logging from multiple threads at the same time might cause double frees and access after free errors.
Create a hello world TITAN project using Eclipse and insert the following code:
Makefile:
...
# Flags for the C++ compiler:
CXXFLAGS = -Wall -O0 -g -pthread
# Flags for the linker:
LDFLAGS = -pthread
...
PCOType.cc:
...
#include <pthread.h>
...
void* thread_main(void* arg) {
for (int i = 0; i < 10000; ++i) {
TTCN_Logger::begin_event(TTCN_USER);
TTCN_Logger::log_event("Side thread: Message");
TTCN_Logger::end_event();
}
return NULL;
}
...
void PCOType::user_map(const char *system_port)
{
...
pthread_t thread;
pthread_create(&thread, NULL, thread_main, NULL);
for (int i = 0; i < 10000; ++i) {
TTCN_Logger::begin_event(TTCN_USER);
TTCN_Logger::log_event("Main thread: Message");
TTCN_Logger::end_event();
}
}
The HTC crashes with a segmentation fault.
Log output is written without memory errors.
Execution log HTC:
TTCN-3 Host Controller (parallel mode), version 7/CAX 105 7730 R2A
./LogThreadingProblem: Segmentation fault occurred
./LogThreadingProblem(+0xd5bd7)[0x56033c942bd7]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x153c0)[0x7fc5352d63c0]
./LogThreadingProblem(+0x36af0a)[0x56033cbd7f0a]
./LogThreadingProblem(+0x624c7)[0x56033c8cf4c7]
./LogThreadingProblem(+0x589c5)[0x56033c8c59c5]
./LogThreadingProblem(+0x2889b)[0x56033c89589b]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x9609)[0x7fc5352ca609]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x43)[0x7fc534ea3293]
We have already fixed the problem with a mutex around the begin_event and end_event functions. I'll provide a fixed version on my fork and add the link as a comment here. Just opening this issue in case someone else has a better solution.
7.2.0 and 8.0.0
Ubuntu 20.04
/cc @aknappqwt
Tobias Erbshäußer (ITK Engineering GmbH) (ee438f11) at 02 Aug 10:05
Custom encoding name with spaces (issue #563)
... and 13 more commits