Commit dced0133 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Add a function to check if the process is in crashing

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
parent 6902012a
...@@ -455,7 +455,13 @@ void innerSignalHandler(int signum, siginfo_t* info, void* /* uctx */) { ...@@ -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) { void signalHandler(int signum, siginfo_t* info, void* uctx) {
gFatalSignalReceived.store(true, std::memory_order_relaxed);
int savedErrno = errno; int savedErrno = errno;
SCOPE_EXIT { SCOPE_EXIT {
flush(); flush();
...@@ -549,5 +555,14 @@ void installFatalSignalHandler(std::bitset<64> signals) { ...@@ -549,5 +555,14 @@ void installFatalSignalHandler(std::bitset<64> signals) {
} }
#endif // FOLLY_USE_SYMBOLIZER #endif // FOLLY_USE_SYMBOLIZER
} }
bool fatalSignalReceived() {
#ifdef FOLLY_USE_SYMBOLIZER
return gFatalSignalReceived.load(std::memory_order_relaxed);
#else
return false;
#endif
}
} // namespace symbolizer } // namespace symbolizer
} // namespace folly } // namespace folly
...@@ -55,5 +55,11 @@ void addFatalSignalCallback(SignalCallback cb); ...@@ -55,5 +55,11 @@ void addFatalSignalCallback(SignalCallback cb);
* callbacks in the order in which they were added. * callbacks in the order in which they were added.
*/ */
void installFatalSignalCallbacks(); void installFatalSignalCallbacks();
/**
* True if a fatal signal was received (i.e. the process is crashing).
*/
bool fatalSignalReceived();
} // namespace symbolizer } // namespace symbolizer
} // namespace folly } // namespace folly
...@@ -42,7 +42,9 @@ void callback1() { ...@@ -42,7 +42,9 @@ void callback1() {
} }
void callback2() { void callback2() {
print("Callback2\n"); if (fatalSignalReceived()) {
print("Callback2\n");
}
} }
[[noreturn]] FOLLY_NOINLINE void funcC() { [[noreturn]] FOLLY_NOINLINE void funcC() {
...@@ -66,6 +68,8 @@ TEST(SignalHandler, Simple) { ...@@ -66,6 +68,8 @@ TEST(SignalHandler, Simple) {
installFatalSignalHandler(); installFatalSignalHandler();
installFatalSignalCallbacks(); installFatalSignalCallbacks();
EXPECT_FALSE(fatalSignalReceived());
EXPECT_DEATH( EXPECT_DEATH(
failHard(), failHard(),
"^\\*\\*\\* Aborted at [0-9]+ \\(Unix time, try 'date -d @[0-9]+'\\) " "^\\*\\*\\* Aborted at [0-9]+ \\(Unix time, try 'date -d @[0-9]+'\\) "
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment