Log4cxx日志记录语句挂起



我们有一个独立的VC++应用程序,我们使用log4cxx0.10.0版本添加了日志记录。应用程序将启动一个线程(对于一些耗时的操作(,如果花费的时间超过阈值时间,则主线程将使用TerminateThread方法终止该线程。子线程函数也有一些日志打印。Log4CXX配置了1 MB大小的5个备份副本的滚动文件附加程序。日志记录在大多数情况下都运行良好。但在某些情况下,主线程日志记录函数调用在杀死子线程后挂起,因此整个应用程序处于挂起状态。应用程序的后续实例也处于挂起状态。我们获取了应用程序的完整崩溃转储,并使用WinDbg进行了分析。

这是应用程序的调用堆栈

**00 ntdll!NtWaitForSingleObject+0xa
01 ntdll!RtlpWaitOnCriticalSection+0xe8
02 ntdll!RtlEnterCriticalSection+0xd1
03 log4cxx!log4cxx::filter::DenyAllFilter::decide+0x194
04 log4cxx!log4cxx::helpers::synchronized::synchronized+0x31
05 log4cxx!log4cxx::Logger::callAppenders+0x81
06 log4cxx!log4cxx::Logger::forcedLog+0xe5**
07 Test!CXX_LOG(int LOG_TYPE = 0n2, char * format = 0x00000001`3f2a2ad8 "Main thread pint...")+0x463 [d:testsaftest.cpp @ 2360]
08 test!TestFunction(int argc = 0n3, char ** argv = 0x00000001`3f2ae880, int level = 0n1)+0x586 [d:testsaftest.cpp @ 1634]
09 test!main(int argc = 0n4, char ** argv = 0x00000000`00282920)+0x1820 [d:testsaftest.cpp @ 2309]
0a test!__tmainCRTStartup(void)+0x13b [f:ddvctoolscrt_bldself_64_amd64crtsrccrt0.c @ 278]
0b kernel32!BaseThreadInitThunk+0xd
0c ntdll!RtlUserThreadStart+0x1d

随后的应用程序挂起以锁定实例的文件和调用堆栈,如下所示

**ntdll!ZwLockFile+0xa
KERNELBASE!LockFileEx+0xb2
kernel32!LockFileEx+0x1b
log4cxx!log4cxx::filter::DenyAllFilter::decide+0x2a89
log4cxx!log4cxx::helpers::DatagramPacket::setData+0x559c
log4cxx!log4cxx::helpers::FileOutputStream::write+0x82
log4cxx!log4cxx::rolling::RollingFileAppenderSkeleton::getTriggeringPolicy+0x1ca
log4cxx!log4cxx::helpers::OutputStreamWriter::write+0xbe
log4cxx!log4cxx::WriterAppender::subAppend+0x7c
log4cxx!log4cxx::rolling::RollingFileAppenderSkeleton::subAppend+0xd0
log4cxx!log4cxx::WriterAppender::append+0x31
log4cxx!log4cxx::AppenderSkeleton::doAppend+0x293
log4cxx!log4cxx::helpers::AppenderAttachableImpl::appendLoopOnAppenders+0x40
log4cxx!log4cxx::Logger::callAppenders+0xa3
log4cxx!log4cxx::Logger::forcedLog+0xe5**
test!CXX_LOG(int LOG_TYPE = 0n2, char * format = 0x00000001`3f2a3868 "Starting the application")+0x463
test!main(int argc = 0n4, char ** argv = 0x00000000`00162920)+0x1806
test!__tmainCRTStartup(void)+0x13b
kernel32!BaseThreadInitThunk+0xd
ntdll!RtlUserThreadStart+0x21

我们已经检查了函数"decision",它与锁定无关。它只是返回一些常数值。我已经读到LOG4CXX是线程安全的。这个问题并不经常发生,因此我们没有以一致的方式复制的步骤。

当我们删除子线程时,有什么需要解决的吗??

重新设计应用程序。TerminateThread本质上是隐含的不安全的,因为线程使用的资源没有被释放。您只是在它持有锁时设法终止了它,现在您的主线程正试图获取持有的锁。找到另一种终止线程的方法。

这是堆栈跟踪中的锁:https://apache.googlesource.com/log4cxx/+/e3db59080a3506f0ed23e98cbcb2be58f0b15a20/src/main/cpp/logger.cpp#93

最新更新