-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
bool m_loggingThreadStopped = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) : | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is .join for the thread later. |
||
{ | ||
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) | ||
{ | ||
|
There was a problem hiding this comment.
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.