malloc in signal handler causes deadloc

stack

Thread 6 (Thread 0x7f2991b3b700 (LWP 26533)):
#0  0x00007f29928e16ac in __lll_lock_wait_private () from /lib64/libc.so.6
#1  0x00007f299285f952 in _L_lock_35 () from /lib64/libc.so.6
#2  0x00007f2992857670 in ptmalloc_lock_all () from /lib64/libc.so.6
#3  0x00007f299289af82 in fork () from /lib64/libc.so.6
#4  0x000000000043b8c7 in ProcessPool::Spawn (this=this@entry=0x7f29840009f0, ctx=...) at build/debug/src/module/kprocesspool/kprocesspool.cpp:406
#5  0x000000000043c06f in ProcessPool::Restart (this=this@entry=0x7f29840009f0, pid=26551) at build/debug/src/module/kprocesspool/kprocesspool.cpp:384
#6  0x000000000043c61a in SignalHandler (signo=<optimized out>) at build/debug/src/module/kprocesspool/kprocesspool.cpp:103
#7  <signal handler called>
#8  0x00007f29925cca82 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#9  0x00007f299354d445 in apsara::PTCond::wait(apsara::PTMutex const&, long) [clone .constprop.116] () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#10 0x00007f299354e29b in apsara::logging::AsyncFileSink::WriteToFile() () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#11 0x00007f299354f753 in std::_Function_handler<apsara::Any (), std::_Bind<std::_Mem_fn<int (apsara::logging::AsyncFileSink::*)()> (apsara::logging::AsyncFileSink*)> >::_M_invoke(std::_Any_data const&) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#12 0x00007f299359bf37 in apsara::ThreadWrapperFunction(void*) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#13 0x00007f29925c8dc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007f29928d3ced in clone () from /lib64/libc.so.6
Thread 5 (Thread 0x7f299133a700 (LWP 26534)):
#0  0x00007f29928e16ac in __lll_lock_wait_private () from /lib64/libc.so.6
#1  0x00007f299285f952 in _L_lock_35 () from /lib64/libc.so.6
#2  0x00007f2992857670 in ptmalloc_lock_all () from /lib64/libc.so.6
#3  0x00007f299289af82 in fork () from /lib64/libc.so.6
#4  0x000000000043b8c7 in ProcessPool::Spawn (this=this@entry=0x7f29840009f0, ctx=...) at build/debug/src/module/kprocesspool/kprocesspool.cpp:406
#5  0x000000000043c06f in ProcessPool::Restart (this=this@entry=0x7f29840009f0, pid=26550) at build/debug/src/module/kprocesspool/kprocesspool.cpp:384
#6  0x000000000043c61a in SignalHandler (signo=<optimized out>) at build/debug/src/module/kprocesspool/kprocesspool.cpp:103
#7  <signal handler called>
#8  0x00007f29925cca82 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#9  0x00007f299354d445 in apsara::PTCond::wait(apsara::PTMutex const&, long) [clone .constprop.116] () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#10 0x00007f299354e29b in apsara::logging::AsyncFileSink::WriteToFile() () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#11 0x00007f299354f753 in std::_Function_handler<apsara::Any (), std::_Bind<std::_Mem_fn<int (apsara::logging::AsyncFileSink::*)()> (apsara::logging::AsyncFileSink*)> >::_M_invoke(std::_Any_data const&) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#12 0x00007f299359bf37 in apsara::ThreadWrapperFunction(void*) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#13 0x00007f29925c8dc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007f29928d3ced in clone () from /lib64/libc.so.6
Thread 4 (Thread 0x7f2990b39700 (LWP 26535)):
#0  0x00007f29925cca82 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00007f299354d445 in apsara::PTCond::wait(apsara::PTMutex const&, long) [clone .constprop.116] () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#2  0x00007f299354e29b in apsara::logging::AsyncFileSink::WriteToFile() () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#3  0x00007f299354f753 in std::_Function_handler<apsara::Any (), std::_Bind<std::_Mem_fn<int (apsara::logging::AsyncFileSink::*)()> (apsara::logging::AsyncFileSink*)> >::_M_invoke(std::_Any_data const&) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#4  0x00007f299359bf37 in apsara::ThreadWrapperFunction(void*) () from /data/wei.xiew/myworks/zark_worker/extralibs/apsara_common-72/lib64/libapsara_common.so.72
#5  0x00007f29925c8dc5 in start_thread () from /lib64/libpthread.so.0
#6  0x00007f29928d3ced in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f2989e11700 (LWP 26545)):

why

malloc() is not a function that can be safely called from a signal handler. It’s not a async-signal-safe function. So, you should never call malloc() from a signal handler.
You are only allowed to call a limited set of functons from a signal handler. See the man signal-safety for the list of functions you can safely call from a signal handler.

refer:

  • https://github.com/ArcticaProject/nx-libs/issues/854
  • https://stackoverflow.com/questions/40049751/malloc-inside-linux-signal-handler-cause-deadlock
  • https://man7.org/linux/man-pages/man7/signal-safety.7.html

你可能感兴趣的:(c/c++,c++)