Skip to content

Commit

Permalink
Add a function to check if the process is in crashing
Browse files Browse the repository at this point in the history
Summary:
This can be used to avoid noisy logging if the process is in the middle of a crash: symbolizing the stack trace can take a long time, so we can end up interleaving the crash report with unrelated logs.

In particular, we can use this to guard monitoring code that is more likely to trip while the process is crashing (for example stall detectors).

Reviewed By: yfeldblum, philippv, luciang

Differential Revision: D27663380

fbshipit-source-id: e3bb40292c3c57579b3eb172847ca089b0f9b07a
  • Loading branch information
ot authored and facebook-github-bot committed Apr 9, 2021
1 parent 6902012 commit dced013
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions folly/experimental/symbolizer/SignalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,13 @@ void innerSignalHandler(int signum, siginfo_t* info, void* /* uctx */) {
}
}

namespace {
std::atomic<bool> gFatalSignalReceived{false};
} // namespace

void signalHandler(int signum, siginfo_t* info, void* uctx) {
gFatalSignalReceived.store(true, std::memory_order_relaxed);

int savedErrno = errno;
SCOPE_EXIT {
flush();
Expand Down Expand Up @@ -549,5 +555,14 @@ void installFatalSignalHandler(std::bitset<64> signals) {
}
#endif // FOLLY_USE_SYMBOLIZER
}

bool fatalSignalReceived() {
#ifdef FOLLY_USE_SYMBOLIZER
return gFatalSignalReceived.load(std::memory_order_relaxed);
#else
return false;
#endif
}

} // namespace symbolizer
} // namespace folly
6 changes: 6 additions & 0 deletions folly/experimental/symbolizer/SignalHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ void addFatalSignalCallback(SignalCallback cb);
* callbacks in the order in which they were added.
*/
void installFatalSignalCallbacks();

/**
* True if a fatal signal was received (i.e. the process is crashing).
*/
bool fatalSignalReceived();

} // namespace symbolizer
} // namespace folly
6 changes: 5 additions & 1 deletion folly/experimental/symbolizer/test/SignalHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ void callback1() {
}

void callback2() {
print("Callback2\n");
if (fatalSignalReceived()) {
print("Callback2\n");
}
}

[[noreturn]] FOLLY_NOINLINE void funcC() {
Expand All @@ -66,6 +68,8 @@ TEST(SignalHandler, Simple) {
installFatalSignalHandler();
installFatalSignalCallbacks();

EXPECT_FALSE(fatalSignalReceived());

EXPECT_DEATH(
failHard(),
"^\\*\\*\\* Aborted at [0-9]+ \\(Unix time, try 'date -d @[0-9]+'\\) "
Expand Down

0 comments on commit dced013

Please sign in to comment.