From f231561276db3f4291b76ada94fbf4a907a44ef7 Mon Sep 17 00:00:00 2001 From: Chris Spiegel Date: Mon, 3 Nov 2025 17:48:04 -0800 Subject: [PATCH] Stop and restart the Prometheus worker thread when daemonizing Also use _Exit() instead of returning from main() to avoid duplicate cleanup/flusing/etc. --- .../core/include/prometheus/save_to_file.h | 10 ++++++++++ one.cpp | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ext/prometheus-cpp-lite-1.0/core/include/prometheus/save_to_file.h b/ext/prometheus-cpp-lite-1.0/core/include/prometheus/save_to_file.h index 26bd0c95c..d181b1358 100644 --- a/ext/prometheus-cpp-lite-1.0/core/include/prometheus/save_to_file.h +++ b/ext/prometheus-cpp-lite-1.0/core/include/prometheus/save_to_file.h @@ -50,6 +50,16 @@ namespace prometheus { public: SaveToFile() = default; + void stop() { + must_die = true; + worker_thread.join(); + } + + void restart() { + must_die = false; + worker_thread = std::thread(&SaveToFile::worker_function, this); + } + ~SaveToFile() { must_die = true; worker_thread.join(); diff --git a/one.cpp b/one.cpp index 0e2ac4cfa..8c67a3af2 100644 --- a/one.cpp +++ b/one.cpp @@ -2330,14 +2330,18 @@ int main(int argc, char** argv) } #endif // !ZT_ONE_NO_ROOT_CHECK if (runAsDaemon) { + prometheus::simpleapi::saver.stop(); + long p = (long)fork(); if (p < 0) { fprintf(stderr, "%s: could not fork" ZT_EOL_S, argv[0]); return 1; } else if (p > 0) - return 0; // forked + _Exit(0); // forked // else p == 0, so we are daemonized + + prometheus::simpleapi::saver.restart(); } #endif // __UNIX_LIKE__