From 1a65284a3f31cc2a4d1f321e6e61e50cddb0c6d8 Mon Sep 17 00:00:00 2001 From: Masaori Koshiba Date: Tue, 7 Apr 2026 13:32:13 +0900 Subject: [PATCH] Flush logs before shutdown --- include/proxy/logging/Log.h | 2 ++ src/proxy/logging/Log.cc | 25 +++++++++++++++++++------ src/traffic_server/traffic_server.cc | 8 ++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/include/proxy/logging/Log.h b/include/proxy/logging/Log.h index ef7df731c0d..480d17b30b4 100644 --- a/include/proxy/logging/Log.h +++ b/include/proxy/logging/Log.h @@ -173,6 +173,8 @@ class Log static int va_error(const char *format, va_list ap); static int error(const char *format, ...) TS_PRINTFLIKE(1, 2); + static void flush_all_objects(); + // public data members static LogObject *error_log; /** The latest fully initialized LogConfig. diff --git a/src/proxy/logging/Log.cc b/src/proxy/logging/Log.cc index 1f0e15bf903..3462bfaefb0 100644 --- a/src/proxy/logging/Log.cc +++ b/src/proxy/logging/Log.cc @@ -1325,6 +1325,14 @@ Log::va_error(const char *format, va_list ap) return ret_val; } +void +Log::flush_all_objects() +{ + if (config) { + config->log_object_manager.flush_all_objects(); + } +} + /*------------------------------------------------------------------------- Log::preproc_thread_main @@ -1343,9 +1351,6 @@ Log::preproc_thread_main(void *args) Log::preproc_notify[idx].lock(); while (true) { - if (TSSystemState::is_event_system_shut_down()) { - return nullptr; - } LogConfig *current = static_cast(configProcessor.get(log_configid)); if (likely(current)) { @@ -1360,6 +1365,13 @@ Log::preproc_thread_main(void *args) configProcessor.release(log_configid, current); } + // Drain any remaining buffers before exiting on shutdown. + if (TSSystemState::is_event_system_shut_down()) { + // Signal flush thread to drain data we just pushed. + Log::flush_notify->signal(); + return nullptr; + } + // wait for more work; a spurious wake-up is ok since we'll just // check the queue and find there is nothing to do, then wait // again. @@ -1384,9 +1396,6 @@ Log::flush_thread_main(void * /* args ATS_UNUSED */) Log::flush_notify->lock(); while (true) { - if (TSSystemState::is_event_system_shut_down()) { - return nullptr; - } fdata = static_cast(ink_atomiclist_popall(flush_data_list)); // invert the list @@ -1478,6 +1487,10 @@ Log::flush_thread_main(void * /* args ATS_UNUSED */) // check the queue and find there is nothing to do, then wait // again. // + if (TSSystemState::is_event_system_shut_down()) { + return nullptr; + } + Log::flush_notify->wait(); } diff --git a/src/traffic_server/traffic_server.cc b/src/traffic_server/traffic_server.cc index 10945ecf246..6790de6892d 100644 --- a/src/traffic_server/traffic_server.cc +++ b/src/traffic_server/traffic_server.cc @@ -287,7 +287,15 @@ struct AutoStopCont : public Continuation { jsonrpcServer->stop_thread(); } + // Push buffered log entries into the preproc queue before shutdown. + Log::flush_all_objects(); + TSSystemState::shut_down_event_system(); + + // Wake preproc threads to drain remaining log buffers before exit. + for (int i = 0; i < Log::preproc_threads; i++) { + Log::preproc_notify[i].signal(); + } delete this; return EVENT_CONT; }