From ab140651684ab8ea62e9d19ba01978cc918eaa25 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 7 Nov 2022 14:59:18 +0300 Subject: [PATCH] Improve fatal error handling in AsyncFileLog. --- tdutils/td/utils/AsyncFileLog.cpp | 10 ++++++++++ tdutils/td/utils/MpscPollableQueue.h | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/tdutils/td/utils/AsyncFileLog.cpp b/tdutils/td/utils/AsyncFileLog.cpp index 3b0f9d28e..9a6e93033 100644 --- a/tdutils/td/utils/AsyncFileLog.cpp +++ b/tdutils/td/utils/AsyncFileLog.cpp @@ -8,8 +8,10 @@ #include "td/utils/port/FileFd.h" #include "td/utils/port/path.h" +#include "td/utils/port/sleep.h" #include "td/utils/port/StdStreams.h" #include "td/utils/SliceBuilder.h" +#include "td/utils/Time.h" namespace td { @@ -137,6 +139,14 @@ void AsyncFileLog::do_append(int log_level, CSlice slice) { process_fatal_error("AsyncFileLog is not inited"); } queue_->writer_put(std::move(query)); + if (log_level == VERBOSITY_NAME(FATAL)) { + // it is not thread-safe to join logging_thread_ there, so just wait for the log line to be printed + auto end_time = Time::now() + 1.0; + while (!queue_->is_empty() && Time::now() < end_time) { + usleep_for(1000); + } + usleep_for(5000); // allow some time for the log line to be actually printed + } } } // namespace td diff --git a/tdutils/td/utils/MpscPollableQueue.h b/tdutils/td/utils/MpscPollableQueue.h index 4daf4a0b9..f6de4bf28 100644 --- a/tdutils/td/utils/MpscPollableQueue.h +++ b/tdutils/td/utils/MpscPollableQueue.h @@ -68,6 +68,11 @@ class MpscPollableQueue { //nop } + bool is_empty() { + auto guard = lock_.lock(); + return writer_vector_.empty() && reader_vector_.empty(); + } + void init() { event_fd_.init(); }