Flush logs unsafely before program fails in the signal handler.
author <shinichiro.hamaji@gmail.com> <>
Fri, 19 Dec 2008 06:49:33 +0000 (06:49 +0000)
committer <shinichiro.hamaji@gmail.com> <>
Fri, 19 Dec 2008 06:49:33 +0000 (06:49 +0000)
git-svn-id: https://google-glog.googlecode.com/svn/trunk@22 eb4d4688-79bd-11dd-afb4-1d65580434c0

src/signalhandler.cc
src/signalhandler_unittest.cc
src/signalhandler_unittest.sh

index 3c89715..367c226 100644 (file)
@@ -279,6 +279,20 @@ void FailureSignalHandler(int signal_number,
     DumpStackFrameInfo("    ", stack[i]);
   }
 #endif
+
+  // *** TRANSITION ***
+  //
+  // BEFORE this point, all code must be async-termination-safe!
+  // (See WARNING above.)
+  //
+  // AFTER this point, we do unsafe things, like using LOG()!
+  // The process could be terminated or hung at any time.  We try to
+  // do more useful things first and riskier things later.
+
+  // Flush the logs before we do anything in case 'anything'
+  // causes problems.
+  FlushLogFilesUnsafe(0);
+
   // Kill ourself by the default signal handler.
   InvokeDefaultSignalHandler(signal_number);
 }
index 48fc713..eb078a0 100644 (file)
@@ -32,6 +32,9 @@ int main(int argc, char **argv) {
   InstallFailureSignalHandler();
   const std::string command = argc > 1 ? argv[1] : "none";
   if (command == "segv") {
+    // We'll check if this is outputted.
+    LOG(INFO) << "create the log file";
+    LOG(INFO) << "a message before segv";
     // We assume 0xDEAD is not writable.
     int *a = (int*)0xDEAD;
     *a = 0;
index ef30450..5875bd2 100755 (executable)
@@ -13,6 +13,7 @@ BINDIR=".libs"
 LIBGLOG="$BINDIR/libglog.so"
 
 BINARY="$BINDIR/signalhandler_unittest"
+LOG_INFO="./signalhandler_unittest.INFO"
 
 # Remove temporary files.
 rm -f signalhandler.out*
@@ -43,12 +44,16 @@ if [ x`uname -p` = x"powerpc" ]; then
 fi
 
 # Test for a case the program kills itself by SIGSEGV.
-$BINARY segv 2> signalhandler.out1
+GOOGLE_LOG_DIR=. $BINARY segv 2> signalhandler.out1
 for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
   if ! grep --quiet "$pattern" signalhandler.out1; then
     die "'$pattern' should appear in the output"
   fi
 done
+if ! grep --quiet "a message before segv" $LOG_INFO; then
+  die "'a message before segv' should appear in the INFO log"
+fi
+rm -f $LOG_INFO
 
 # Test for a case the program is killed by this shell script.
 # $! = the process id of the last command run in the background.