From 5323480780f6a52549e79360ee61f8d76cc9e34b Mon Sep 17 00:00:00 2001 From: GergoTot <54616254+GergoTot@users.noreply.github.com> Date: Wed, 8 Mar 2023 01:30:58 +0100 Subject: [PATCH] Avoid pending of containerized applications in case of aborting (#481) Improvement for Docker run C++ applications with g3log * This addresses the case when a PID1 process crashes and the signal handling, goes into multiple or even infinite loops due to subsequent crashes. The PR makes sure to restore all signal handlers to the original signal handling after the first crash is detected. For --- src/crashhandler_unix.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/crashhandler_unix.cpp b/src/crashhandler_unix.cpp index 09e4c0e..df18b23 100644 --- a/src/crashhandler_unix.cpp +++ b/src/crashhandler_unix.cpp @@ -62,15 +62,16 @@ namespace { // ALL thanks to this thread at StackOverflow. Pretty much borrowed from: // Ref: http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes void signalHandler(int signal_number, siginfo_t* /*info*/, void* /*unused_context*/) { - + + using namespace g3::internal; + // Only one signal will be allowed past this point if (false == shouldDoExit()) { - while (true) { + while (shouldBlockForFatalHandling()) { std::this_thread::sleep_for(std::chrono::seconds(1)); } } - using namespace g3::internal; { const auto dump = stackdump(); std::ostringstream fatal_stream; @@ -233,7 +234,13 @@ namespace g3 { // --- If LOG(FATAL) or CHECK(false) the signal_number will be SIGABRT void exitWithDefaultSignalHandler(const LEVELS& level, g3::SignalType fatal_signal_id) { const int signal_number = static_cast(fatal_signal_id); - restoreSignalHandler(signal_number); + + // Restore all saved signal handlers. If handling a signal which causes exiting + // than let the original signal handlers to handle other signals. + for (const auto& sig : gSignals) { + restoreSignalHandler(sig.first); + } + std::cerr << "\n\n" << __FUNCTION__ << ":" << __LINE__ << ". Exiting due to " << level.text << ", " << signal_number << " \n\n" << std::flush;