Prevent deadlock while printing log records 96/319096/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 13 Nov 2024 10:37:03 +0000 (19:37 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Tue, 4 Feb 2025 07:29:45 +0000 (16:29 +0900)
Change-Id: Ie570fb3bd9792f794eae7e0ef963654d4a2eb7f8

wakeup-manager/src/log_recorder.cpp

index 0c5c7af99eeb4270644d1571469e18d41a6baa52..2aaab5373cb667d8d925a84d26a5c833d4e73e9b 100644 (file)
@@ -104,12 +104,22 @@ void LogRecorder::initialize()
                 std::this_thread::sleep_for(std::chrono::seconds(1));
                 mCounter.fetch_add(1);
                 if (mCounter.load() > MAX_TIMER_COUNTER) {
+                    std::unique_lock<std::mutex> lock(mMutex, std::defer_lock);
+
                     mIsPrinting.store(true);
                     dlog_print(DLOG_ERROR, LOG_TAG, "Counter : %d, printing recorded logs ===", mCounter.load());
                     std::cerr << "Counter : " << mCounter.load() << "printing recorded logs ===" << std::endl;
-                    std::lock_guard<std::mutex> lock(mMutex);
                     int index = 0;
-                    for (auto message : mMainMessages) {
+
+                    std::list<std::string> mainMessages;
+                    std::list<std::string> allMessages;
+
+                    lock.lock();
+                    std::copy(mMainMessages.begin(), mMainMessages.end(), std::back_inserter(mainMessages));
+                    std::copy(mAllMessages.begin(), mAllMessages.end(), std::back_inserter(allMessages));
+                    lock.unlock();
+
+                    for (auto message : mainMessages) {
                         std::this_thread::sleep_for(std::chrono::milliseconds(10));
                         dlog_print(DLOG_ERROR, LOG_TAG, "M[%d], %s", ++index, message.c_str());
                         std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -119,7 +129,7 @@ void LogRecorder::initialize()
                         }
                     }
                     index = 0;
-                    for (auto message : mAllMessages) {
+                    for (auto message : allMessages) {
                         std::this_thread::sleep_for(std::chrono::milliseconds(10));
                         dlog_print(DLOG_ERROR, LOG_TAG, "A[%d], %s", ++index, message.c_str());
                         if (mCounter <= 0) {