From 7962b24aec4ab87e5febbaf3f54ed38751dd1486 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Fri, 20 Nov 2026 16:29:05 +0300 Subject: [PATCH] osd: Stop on SIGINT/SIGTERM from the main loop, not the signal handler The handler called osd_t::force_stop(), which builds a JSON object, allocates strings and starts an HTTP request to etcd. None of that is async-signal-safe. When the signal interrupts malloc() or free() in the main loop, the handler re-enters the allocator and corrupts the heap, and the OSD dies in malloc with SIGSEGV or SIGABRT instead of exiting with 0. Only set a flag in the handler, like the NBD, UBLK and NFS servers do, and call force_stop() from the main loop. A second signal still exits at once, now with _exit(). --- src/osd/osd_main.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/osd/osd_main.cpp b/src/osd/osd_main.cpp index ab18f273..4ed34988 100644 --- a/src/osd/osd_main.cpp +++ b/src/osd/osd_main.cpp @@ -7,19 +7,19 @@ #include #include +#include static osd_t *osd = NULL; -static bool force_stopping = false; +static volatile sig_atomic_t stop_signal = 0; +// Only async-signal-safe calls are allowed here, the OSD is stopped by the main loop static void handle_sigint(int sig) { - if (osd && !force_stopping) + if (stop_signal) { - force_stopping = true; - osd->force_stop(0); - return; + _exit(0); } - exit(0); + stop_signal = 1; } static const char* help_text = @@ -83,9 +83,15 @@ int main(int narg, char *args[]) return 1; } osd->start(); + bool force_stopping = false; while (1) { ringloop->loop(); + if (stop_signal && !force_stopping) + { + force_stopping = true; + osd->force_stop(0); + } ringloop->wait(); } delete osd; -- 2.47.3