Skip to content
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

Prevent undefined behaviour during process exit #285

Merged
merged 3 commits into from
Nov 10, 2023

Conversation

swebb2066
Copy link
Contributor

@swebb2066 swebb2066 commented Nov 9, 2023

When some background threads call LogLog::debug on exit which can be after the static LogLogPrivate pointer is deleted by the onexit processing chain.

PR #219 was not sufficient to fix this because the raw pointer held by std::unique_pointer is not zeroed by the std::unique_pointer destructor.

@swebb2066
Copy link
Contributor Author

swebb2066 commented Nov 10, 2023

For the record, this PR fixes the following deadlock in multithreadtest, when using XMLSocketAppender to connect to a TCP port that does not have a server process listening.
`(gdb) thread apply all bt

Thread 5 (Thread 0x7f4287fff640 (LWP 7247) "multithreadtest"):
#0 futex_wait (private=0, expected=2, futex_word=0x561a8dbb2c08) at ../sysdeps/nptl/futex-internal.h:146
#1 __GI___lll_lock_wait (futex=futex@entry=0x561a8dbb2c08, private=0) at ./nptl/lowlevellock.c:49
#2 0x00007f428e14805d in lll_mutex_lock_optimized (mutex=0x561a8dbb2c08) at ./nptl/pthread_mutex_lock.c:48
#3 ___pthread_mutex_lock (mutex=0x561a8dbb2c08) at ./nptl/pthread_mutex_lock.c:128
#4 0x00007f428e67e68e in log4cxx::helpers::AppenderAttachableImpl::appendLoopOnAppenders(std::shared_ptrlog4cxx::spi::LoggingEvent const&, log4cxx::helpers::Pool&) () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#5 0x00007f428e6e7403 in log4cxx::Logger::callAppenders(std::shared_ptrlog4cxx::spi::LoggingEvent const&, log4cxx::helpers::Pool&) const () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#6 0x00007f428e6e8170 in log4cxx::Logger::addEvent(std::shared_ptrlog4cxx::Level const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >&&, log4cxx::spi::LocationInfo const&) const () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#7 0x0000561a8c6c2944 in multithread_logger(int) ()
#8 0x00007f428e3d4253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#9 0x00007f428e144ac3 in start_thread (arg=) at ./nptl/pthread_create.c:442
#10 0x00007f428e1d6a40 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

Thread 4 (Thread 0x7f428c9a8640 (LWP 7246) "multithreadtest"):
#0 __futex_abstimed_wait_common64 (private=128, cancel=true, abstime=0x0, op=265, expected=7244, futex_word=0x7f428d9aa910) at ./nptl/futex-internal.c:57
#1 __futex_abstimed_wait_common (cancel=true, private=128, abstime=0x0, clockid=0, expected=7244, futex_word=0x7f428d9aa910) at ./nptl/futex-internal.c:87
#2 __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x7f428d9aa910, expected=7244, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=128) at ./nptl/futex-internal.c:139
#3 0x00007f428e146624 in __pthread_clockjoin_ex (threadid=139923820291648, thread_return=0x0, clockid=0, abstime=0x0, block=) at ./nptl/pthread_join_common.c:105
#4 0x00007f428e3d42c7 in std::thread::join() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007f428e7572d9 in log4cxx::net::SocketAppenderSkeleton::close() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#6 0x00007f428e67ea71 in log4cxx::helpers::AppenderAttachableImpl::removeAllAppenders() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#7 0x00007f428e6c8c84 in log4cxx::Hierarchy::~Hierarchy() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#8 0x00007f428e6cdaba in std::_Sp_counted_ptr<log4cxx::Hierarchy*, (__gnu_cxx::_Lock_policy)2>::_M_dispose() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#9 0x00007f428e6a3745 in log4cxx::spi::DefaultRepositorySelector::~DefaultRepositorySelector() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#10 0x00007f428e68386a in log4cxx::helpers::APRInitializer::~APRInitializer() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#11 0x00007f428e0f5495 in __run_exit_handlers (status=0, listp=0x7f428e2c9838 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at ./stdlib/exit.c:113
#12 0x00007f428e0f5610 in __GI_exit (status=) at ./stdlib/exit.c:143
#13 0x00007f428e149ee8 in __pthread_once_slow (once_control=0x561a8c6ce070 <multithread_logger(int)::exiting>, init_routine=0x7f428e3d2d50 <__once_proxy>) at ./nptl/pthread_once.c:116
#14 0x0000561a8c6c2a0b in multithread_logger(int) ()
#15 0x00007f428e3d4253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#16 0x00007f428e144ac3 in start_thread (arg=) at ./nptl/pthread_create.c:442
#17 0x00007f428e1d6a40 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

Thread 3 (Thread 0x7f428d1a9640 (LWP 7245) "multithreadtest"):
#0 futex_wait (private=0, expected=2, futex_word=0x561a8dbb2c08) at ../sysdeps/nptl/futex-internal.h:146
#1 __GI___lll_lock_wait (futex=futex@entry=0x561a8dbb2c08, private=0) at ./nptl/lowlevellock.c:49
#2 0x00007f428e14805d in lll_mutex_lock_optimized (mutex=0x561a8dbb2c08) at ./nptl/pthread_mutex_lock.c:48
#3 ___pthread_mutex_lock (mutex=0x561a8dbb2c08) at ./nptl/pthread_mutex_lock.c:128
#4 0x00007f428e67e68e in log4cxx::helpers::AppenderAttachableImpl::appendLoopOnAppenders(std::shared_ptrlog4cxx::spi::LoggingEvent const&, log4cxx::helpers::Pool&) () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#5 0x00007f428e6e7403 in log4cxx::Logger::callAppenders(std::shared_ptrlog4cxx::spi::LoggingEvent const&, log4cxx::helpers::Pool&) const () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#6 0x00007f428e6e8170 in log4cxx::Logger::addEvent(std::shared_ptrlog4cxx::Level const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >&&, log4cxx::spi::LocationInfo const&) const () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#7 0x0000561a8c6c2944 in multithread_logger(int) ()
#8 0x00007f428e3d4253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#9 0x00007f428e144ac3 in start_thread (arg=) at ./nptl/pthread_create.c:442
#10 0x00007f428e1d6a40 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

Thread 2 (Thread 0x7f428d9aa640 (LWP 7244) "multithreadtest"):
#0 futex_wait (private=0, expected=2, futex_word=0x561a8dbb2948) at ../sysdeps/nptl/futex-internal.h:146
#1 __GI___lll_lock_wait (futex=futex@entry=0x561a8dbb2948, private=0) at ./nptl/lowlevellock.c:49
#2 0x00007f428e148002 in lll_mutex_lock_optimized (mutex=0x561a8dbb2948) at ./nptl/pthread_mutex_lock.c:48
#3 ___pthread_mutex_lock (mutex=0x561a8dbb2948) at ./nptl/pthread_mutex_lock.c:93
#4 0x00007f428e6f1c10 in log4cxx::helpers::LogLog::debug(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) () from /home/stephen/build/logging-log4cxx/src/main/cpp/libl--Type for more, q to quit, c to continue without paging--c
og4cxx.so.15
#5 0x00007f428e759745 in log4cxx::net::SocketAppenderSkeleton::monitor() () from /home/stephen/build/logging-log4cxx/src/main/cpp/liblog4cxx.so.15
#6 0x00007f428e3d4253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007f428e144ac3 in start_thread (arg=) at ./nptl/pthread_create.c:442
#8 0x00007f428e1d6a40 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

Thread 1 (Thread 0x7f428df20b80 (LWP 7243) "multithreadtest"):
#0 __futex_abstimed_wait_common64 (private=128, cancel=true, abstime=0x0, op=265, expected=7245, futex_word=0x7f428d1a9910) at ./nptl/futex-internal.c:57
#1 __futex_abstimed_wait_common (cancel=true, private=128, abstime=0x0, clockid=0, expected=7245, futex_word=0x7f428d1a9910) at ./nptl/futex-internal.c:87
#2 __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x7f428d1a9910, expected=7245, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=128) at ./nptl/futex-internal.c:139
#3 0x00007f428e146624 in __pthread_clockjoin_ex (threadid=139923811898944, thread_return=0x0, clockid=0, abstime=0x0, block=) at ./nptl/pthread_join_common.c:105
#4 0x00007f428e3d42c7 in std::thread::join() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x0000561a8c6c2237 in MultithreadTest::testMultithreadedLoggers() ()
#6 0x0000561a8c6c1d42 in void LogUnit::runTest(abts_case*, void (MultithreadTest::)()) ()
#7 0x0000561a8c6c4630 in abts_run_test(abts_suite
, char const*, void ()(abts_case, void*), void*) ()
#8 0x0000561a8c6c5c31 in abts_run_suites(abts_suite*) ()
#9 0x0000561a8c6c0bd0 in main ()
(gdb)
`

@swebb2066 swebb2066 merged commit 544727f into master Nov 10, 2023
24 checks passed
@swebb2066 swebb2066 deleted the loglog_robustness_2 branch November 10, 2023 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants