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

LOGCXX-537 avoid deadlock if socket fails #82

Merged
merged 5 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/cpp/appenderskeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AppenderSkeleton::AppenderSkeleton()
tailFilter(),
pool()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
closed = false;
}

Expand All @@ -52,7 +52,7 @@ AppenderSkeleton::AppenderSkeleton(const LayoutPtr& layout1)
tailFilter(),
pool()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
closed = false;
}

Expand All @@ -70,7 +70,7 @@ void AppenderSkeleton::finalize()

void AppenderSkeleton::addFilter(const spi::FilterPtr& newFilter)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (headFilter == 0)
{
Expand All @@ -85,7 +85,7 @@ void AppenderSkeleton::addFilter(const spi::FilterPtr& newFilter)

void AppenderSkeleton::clearFilters()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
headFilter = tailFilter = 0;
}

Expand All @@ -96,7 +96,7 @@ bool AppenderSkeleton::isAsSevereAsThreshold(const LevelPtr& level) const

void AppenderSkeleton::doAppend(const spi::LoggingEventPtr& event, Pool& pool1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

doAppendImpl(event, pool1);
}
Expand Down Expand Up @@ -139,7 +139,7 @@ void AppenderSkeleton::doAppendImpl(const spi::LoggingEventPtr& event, Pool& poo

void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (errorHandler1 == 0)
{
Expand All @@ -155,7 +155,7 @@ void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1)

void AppenderSkeleton::setThreshold(const LevelPtr& threshold1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
this->threshold = threshold1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void AsyncAppender::setOption(const LogString& option,

void AsyncAppender::doAppend(const spi::LoggingEventPtr& event, Pool& pool1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

doAppendImpl(event, pool1);
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/cpp/fileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ IMPLEMENT_LOG4CXX_OBJECT(FileAppender)

FileAppender::FileAppender()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileAppend = true;
bufferedIO = false;
bufferSize = 8 * 1024;
Expand All @@ -47,7 +47,7 @@ FileAppender::FileAppender(const LayoutPtr& layout1, const LogString& fileName1,
: WriterAppender(layout1)
{
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileAppend = append1;
fileName = fileName1;
bufferedIO = bufferedIO1;
Expand All @@ -62,7 +62,7 @@ FileAppender::FileAppender(const LayoutPtr& layout1, const LogString& fileName1,
: WriterAppender(layout1)
{
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileAppend = append1;
fileName = fileName1;
bufferedIO = false;
Expand All @@ -76,7 +76,7 @@ FileAppender::FileAppender(const LayoutPtr& layout1, const LogString& fileName1)
: WriterAppender(layout1)
{
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileAppend = true;
fileName = fileName1;
bufferedIO = false;
Expand All @@ -93,13 +93,13 @@ FileAppender::~FileAppender()

void FileAppender::setAppend(bool fileAppend1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
this->fileAppend = fileAppend1;
}

void FileAppender::setFile(const LogString& file)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
setFileInternal(file);
}

Expand All @@ -110,7 +110,7 @@ void FileAppender::setFileInternal(const LogString& file)

void FileAppender::setBufferedIO(bool bufferedIO1)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
this->bufferedIO = bufferedIO1;

if (bufferedIO1)
Expand All @@ -125,27 +125,27 @@ void FileAppender::setOption(const LogString& option,
if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("FILE"), LOG4CXX_STR("file"))
|| StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("FILENAME"), LOG4CXX_STR("filename")))
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileName = stripDuplicateBackslashes(value);
}
else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("APPEND"), LOG4CXX_STR("append")))
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
fileAppend = OptionConverter::toBoolean(value, true);
}
else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("BUFFEREDIO"), LOG4CXX_STR("bufferedio")))
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
bufferedIO = OptionConverter::toBoolean(value, true);
}
else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("IMMEDIATEFLUSH"), LOG4CXX_STR("immediateflush")))
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
bufferedIO = !OptionConverter::toBoolean(value, false);
}
else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("BUFFERSIZE"), LOG4CXX_STR("buffersize")))
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
bufferSize = OptionConverter::toFileSize(value, 8 * 1024);
}
else
Expand All @@ -156,7 +156,7 @@ void FileAppender::setOption(const LogString& option,

void FileAppender::activateOptions(Pool& p)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
activateOptionsInternal(p);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/rollingfileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void RollingFileAppenderSkeleton::activateOptions(Pool& p)
}

{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
triggeringPolicy->activateOptions(p);
rollingPolicy->activateOptions(p);

Expand Down Expand Up @@ -183,7 +183,7 @@ void RollingFileAppenderSkeleton::releaseFileLock(apr_file_t* lock_file)
*/
bool RollingFileAppenderSkeleton::rollover(Pool& p)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
return rolloverInternal(p);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/socketappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int SocketAppender::getDefaultPort() const

void SocketAppender::setSocket(log4cxx::helpers::SocketPtr& socket, Pool& p)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

SocketOutputStreamPtr sock = SocketOutputStreamPtr(new SocketOutputStream(socket));
oos = ObjectOutputStreamPtr(new ObjectOutputStream(sock, p));
Expand Down
24 changes: 14 additions & 10 deletions src/main/cpp/socketappenderskeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void SocketAppenderSkeleton::activateOptions(Pool& p)

void SocketAppenderSkeleton::close()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (closed)
{
Expand Down Expand Up @@ -157,7 +157,14 @@ void SocketAppenderSkeleton::setOption(const LogString& option, const LogString&

void SocketAppenderSkeleton::fireConnector()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if( thread.joinable() ){
// This should only get called if the thread has already exited.
// We could have a potential bug if fireConnector is called while
// the thread is waiting for a connection
thread.join();
}

if ( !thread.joinable() )
{
Expand All @@ -176,12 +183,6 @@ void SocketAppenderSkeleton::monitor()
{
try
{
std::this_thread::sleep_for( std::chrono::milliseconds( reconnectionDelay ) );

std::unique_lock<std::mutex> lock( interrupt_mutex );
interrupt.wait_for( lock, std::chrono::milliseconds( reconnectionDelay ),
std::bind(&SocketAppenderSkeleton::is_closed, this) );

if (!closed)
{
LogLog::debug(LogString(LOG4CXX_STR("Attempting connection to "))
Expand All @@ -190,9 +191,8 @@ void SocketAppenderSkeleton::monitor()
Pool p;
setSocket(socket, p);
LogLog::debug(LOG4CXX_STR("Connection established. Exiting connector thread."));
return;
}

return;
}
catch (InterruptedException&)
{
Expand All @@ -216,6 +216,10 @@ void SocketAppenderSkeleton::monitor()
+ exmsg);
}

std::unique_lock<std::mutex> lock( interrupt_mutex );
interrupt.wait_for( lock, std::chrono::milliseconds( reconnectionDelay ),
std::bind(&SocketAppenderSkeleton::is_closed, this) );

isClosed = closed;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/sockethubappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void SocketHubAppender::setOption(const LogString& option,
void SocketHubAppender::close()
{
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (closed)
{
Expand All @@ -104,7 +104,7 @@ void SocketHubAppender::close()
thread.join();
}

std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
// close all of the connections
LogLog::debug(LOG4CXX_STR("closing client connections"));

Expand Down Expand Up @@ -234,7 +234,7 @@ void SocketHubAppender::monitor()
+ LOG4CXX_STR(")"));

// add it to the oosList.
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
OutputStreamPtr os(new SocketOutputStream(socket));
Pool p;
ObjectOutputStreamPtr oos(new ObjectOutputStream(os, p));
Expand Down
10 changes: 5 additions & 5 deletions src/main/cpp/telnetappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ void TelnetAppender::setOption(const LogString& option,

LogString TelnetAppender::getEncoding() const
{
log4cxx::shared_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
return encoding;
}

void TelnetAppender::setEncoding(const LogString& value)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
encoder = CharsetEncoder::getEncoder(value);
encoding = value;
}


void TelnetAppender::close()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (closed)
{
Expand Down Expand Up @@ -195,7 +195,7 @@ void TelnetAppender::append(const spi::LoggingEventPtr& event, Pool& p)
LogString::const_iterator msgIter(msg.begin());
ByteBuffer buf(bytes, bytesSize);

log4cxx::shared_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

while (msgIter != msg.end())
{
Expand Down Expand Up @@ -251,7 +251,7 @@ void TelnetAppender::acceptConnections()
//
// find unoccupied connection
//
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

for (ConnectionList::iterator iter = connections.begin();
iter != connections.end();
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/writerappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ bool WriterAppender::checkEntryConditions() const
*/
void WriterAppender::close()
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);

if (closed)
{
Expand Down Expand Up @@ -278,7 +278,7 @@ void WriterAppender::writeHeader(Pool& p)

void WriterAppender::setWriter(const WriterPtr& newWriter)
{
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
setWriterInternal(newWriter);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/xmlsocketappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void XMLSocketAppender::setSocket(log4cxx::helpers::SocketPtr& socket, Pool& p)
{
OutputStreamPtr os(new SocketOutputStream(socket));
CharsetEncoderPtr charset(CharsetEncoder::getUTF8Encoder());
std::unique_lock<log4cxx::shared_mutex> lock(mutex);
std::lock_guard<std::recursive_mutex> lock(mutex);
writer = OutputStreamWriterPtr(new OutputStreamWriter(os, charset));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/include/log4cxx/appenderskeleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class LOG4CXX_EXPORT AppenderSkeleton :
bool closed;

log4cxx::helpers::Pool pool;
mutable log4cxx::shared_mutex mutex;
mutable std::recursive_mutex mutex;

/**
Subclasses of <code>AppenderSkeleton</code> should implement this
Expand Down
Loading