Skip to content

Commit

Permalink
Avoid pending of containerized applications in case of aborting (#481)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GergoTot authored Mar 8, 2023
1 parent 4f1224b commit 5323480
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/crashhandler_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int>(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;


Expand Down

0 comments on commit 5323480

Please sign in to comment.