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

Avoid processing log statement if logger has been destructed #2833

Merged
merged 1 commit into from
Jan 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ namespace Aws
struct LogSynchronizationData
{
public:
LogSynchronizationData() : m_stopLogging(false) {}
LogSynchronizationData() : m_stopLogging(false), m_loggingThreadStopped(false) {}

std::mutex m_logQueueMutex;
std::condition_variable m_queueSignal;
Aws::Vector<Aws::String> m_queuedLogMessages;
bool m_stopLogging;
bool m_stopLogging = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant to the constructor at line 61.

bool m_loggingThreadStopped = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be initialized in the constructor instead?


private:
LogSynchronizationData(const LogSynchronizationData& rhs) = delete;
Expand Down
21 changes: 21 additions & 0 deletions src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ static void LogThread(DefaultLogSystem::LogSynchronizationData* syncData, std::s
logFile->flush();
}
}

{
std::unique_lock<std::mutex> locker(syncData->m_logQueueMutex);
syncData->m_loggingThreadStopped = true;
syncData->m_queueSignal.notify_one();
}
}

DefaultLogSystem::DefaultLogSystem(LogLevel logLevel, const std::shared_ptr<Aws::OStream>& logFile) :
Expand All @@ -90,12 +96,27 @@ DefaultLogSystem::~DefaultLogSystem()
m_syncData.m_queueSignal.notify_one();
}

// explicitly wait for logging thread to finish
{
std::unique_lock<std::mutex> locker(m_syncData.m_logQueueMutex);
if (!m_syncData.m_loggingThreadStopped)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "while" instead of "if" and not use a conditional wait_for? It is not safe to leave the destructor until we know the thread is gone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is .join for the thread later.
I just did not want to block user code if the boolean is never set.

{
m_syncData.m_queueSignal.wait_for(locker,
std::chrono::milliseconds(500),
[&](){ return m_syncData.m_loggingThreadStopped; });
}
}

m_loggingThread.join();
}

void DefaultLogSystem::ProcessFormattedStatement(Aws::String&& statement)
{
std::lock_guard<std::mutex> locker(m_syncData.m_logQueueMutex);
if (m_syncData.m_stopLogging)
{
return;
}
m_syncData.m_queuedLogMessages.emplace_back(std::move(statement));
if(m_syncData.m_queuedLogMessages.size() >= BUFFERED_MSG_COUNT)
{
Expand Down
Loading