From 65635ddd77aee2dcd1375d9ffec87f14b6d9c1b1 Mon Sep 17 00:00:00 2001 From: Wang Zhiyong Date: Wed, 16 Oct 2024 19:32:59 +0800 Subject: [PATCH] Capture more system signals (#705) * Capture more system signals * fix cpplit --- src/server/lgraph_server.cpp | 52 ++++++++++++++++++++++++++---------- src/server/service.h | 3 +-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/server/lgraph_server.cpp b/src/server/lgraph_server.cpp index 9328be8ffb..f989fc5796 100644 --- a/src/server/lgraph_server.cpp +++ b/src/server/lgraph_server.cpp @@ -44,27 +44,50 @@ DECLARE_bool(usercode_in_pthread); namespace lgraph { static Signal _kill_signal_; -void int_handler(int x) { - LOG_INFO() << "!!!!! Received signal " << x << ", exiting... !!!!!"; +void shutdown_handler(int sig) { + LOG_WARN() << FMA_FMT("Received signal {}, shutdown", std::string(strsignal(sig))); + lgraph_log::LoggerManager::GetInstance().FlushAllSinks(); _kill_signal_.Notify(); } +void crash_handler(int sig) { + LOG_ERROR() << FMA_FMT("Received signal {}, crash", std::string(strsignal(sig))); + lgraph_log::LoggerManager::GetInstance().FlushAllSinks(); + + struct sigaction sa{}; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_DFL; + sigaction(sig, &sa, nullptr); + kill(getpid(), sig); +} + #ifndef _WIN32 #include static void SetupSignalHandler() { - struct sigaction sa {}; - sa.sa_handler = int_handler; - sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - int r = sigaction(SIGINT, &sa, nullptr); - if (r != 0) { - LOG_ERROR() << "Error setting up SIGINT signal handler: " << strerror(errno); + { + // shutdown + struct sigaction sa{}; + sa.sa_handler = shutdown_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SIGINT, &sa, nullptr); + sigaction(SIGTERM, &sa, nullptr); + sigaction(SIGUSR1, &sa, nullptr); } - r = sigaction(SIGQUIT, &sa, nullptr); - if (r != 0) { - LOG_ERROR() << "Error setting up SIGQUIT signal handler: " << strerror(errno); + { + // crash + struct sigaction sa{}; + sa.sa_handler = crash_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_NODEFER; + sigaction(SIGSEGV, &sa, nullptr); + sigaction(SIGBUS, &sa, nullptr); + sigaction(SIGFPE, &sa, nullptr); + sigaction(SIGILL, &sa, nullptr); + sigaction(SIGABRT, &sa, nullptr); } } @@ -167,12 +190,13 @@ int LGraphServer::Start() { LOG_INFO() << header.str(); struct rlimit rlim{}; getrlimit(RLIMIT_CORE, &rlim); - LOG_INFO() << "The maximum limit of core file size is " << rlim.rlim_cur; + LOG_INFO() << FMA_FMT("Core dump file limit size, soft limit: {}, hard limit: {}", + rlim.rlim_cur, rlim.rlim_max); std::ifstream file("/proc/sys/kernel/core_pattern"); if (file.is_open()) { std::string content((std::istreambuf_iterator(file)), (std::istreambuf_iterator())); - LOG_INFO() << "The path to the core file is " << content; + LOG_INFO() << "Core dump file path: " << content; file.close(); } diff --git a/src/server/service.h b/src/server/service.h index 078969276c..a9f939eaf6 100644 --- a/src/server/service.h +++ b/src/server/service.h @@ -213,8 +213,7 @@ class Service { LOG_WARN() << "Service " << service_name_ << " is already dead."; return NO_PID_FILE; } - // int r = kill(pid, SIGINT); - int r = kill(pid, SIGQUIT); + int r = kill(pid, SIGUSR1); if (r == EPERM) { LOG_WARN() << "Failed to kill service " << service_name_ << ": " << strerror(errno); return OTHER_ERROR;