From d96d4f6a2782fd5d3e9ba12279b06bf9e681021e Mon Sep 17 00:00:00 2001 From: Jash Date: Tue, 17 Nov 2026 08:54:50 +0530 Subject: [PATCH] fix: zmc logs prime failures once per 60 and keeps a SIGHUP that arrives during reload refs #6531 --- src/zmc.cpp | 6 +++--- tests/zm_zmc_main.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/zmc.cpp b/src/zmc.cpp index d315f07c31..10d1e39c34 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -313,7 +313,7 @@ int main(int argc, char *argv[]) { Seconds sleep_time = Seconds(0); bool priming_failed = false; while ((monitor->PrimeCapture() <= 0) and !zm_terminate) { - if (prime_capture_log_count % 60) { + if (prime_capture_log_count % 60 == 0) { logPrintf(Logger::ERROR + monitor->Importance(), "Failed to prime capture of initial monitor"); } else { Debug(1, "Failed to prime capture of initial monitor"); @@ -503,6 +503,8 @@ int main(int argc, char *argv[]) { } if (zm_reload) { + // Clear first so a SIGHUP that arrives while reloading is not lost. + zm_reload = false; zmLoadStaticConfig(); zmLoadDBConfig(); for (std::shared_ptr &monitor : monitors) { @@ -510,8 +512,6 @@ int main(int argc, char *argv[]) { } // The dbQueue and stream socket threads are still running and logging. logReload(log_id_string); - - zm_reload = false; } // end if zm_reload } // end while ! zm_terminate outer connection loop diff --git a/tests/zm_zmc_main.cpp b/tests/zm_zmc_main.cpp index 33868043c7..0fb55f98ba 100644 --- a/tests/zm_zmc_main.cpp +++ b/tests/zm_zmc_main.cpp @@ -16,6 +16,8 @@ #include "zm_monitor.h" #include +#include +#include #include #include #include @@ -74,3 +76,35 @@ TEST_CASE("interruptible sleep stops when asked and keeps ticking", "[zmcmain]") REQUIRE(std::chrono::steady_clock::now() - start < std::chrono::seconds(2)); REQUIRE(InterruptibleSleep(std::chrono::milliseconds(150), []() { return false; }, nullptr)); } + +namespace { +std::string ReadZmcSource() { + std::ifstream in(std::string(ZM_SOURCE_DIR) + "/src/zmc.cpp"); + std::stringstream ss; + ss << in.rdbuf(); + return ss.str(); +} +} // namespace + +TEST_CASE("prime failure logs an error on the first failure and every 60th", "[zmcmain]") { + const std::string src = ReadZmcSource(); + const auto pos = src.find("Failed to prime capture of initial monitor"); + REQUIRE(pos != std::string::npos); + const std::string before = src.substr(pos > 200 ? pos - 200 : 0, pos > 200 ? 200 : pos); + REQUIRE(before.find("prime_capture_log_count % 60 == 0") != std::string::npos); +} + +TEST_CASE("zmc clears zm_reload before reloading so a SIGHUP during reload is kept", "[zmcmain]") { + const std::string src = ReadZmcSource(); + const auto start = src.find("if (zm_reload) {\n // Clear first"); + REQUIRE(start != std::string::npos); + const auto reload = src.find("monitor->Reload();", start); + REQUIRE(reload != std::string::npos); + const auto clear = src.find("zm_reload = false;", start); + REQUIRE(clear != std::string::npos); + REQUIRE(clear < reload); + // No clear after the reload finished, which would drop a SIGHUP that arrived meanwhile. + const auto end = src.find("end if zm_reload", reload); + REQUIRE(end != std::string::npos); + REQUIRE(src.substr(reload, end - reload).find("zm_reload = false") == std::string::npos); +}