From 1829a35cd0413a752bad082967508ffd8b1c7d01 Mon Sep 17 00:00:00 2001 From: Mihai Moldovan Date: Sat, 30 Sep 2017 15:32:33 +0200 Subject: nxcomp/src/Log.h: block signals while writing out data. Prevents race conditions caused by signal handlers while flushing out our log queue. --- nxcomp/src/Log.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'nxcomp/src') diff --git a/nxcomp/src/Log.h b/nxcomp/src/Log.h index ecc6ea9ed..e3b1397d2 100644 --- a/nxcomp/src/Log.h +++ b/nxcomp/src/Log.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -218,6 +219,22 @@ class NXLog */ void flush(per_thread_data *pdt) { + /* + * Block all signals until we are dong printing data. + * Ensures that a signal handler won't interrupt us + * and overwrite the buffer data mid-print, leading + * to confusing output. + */ + sigset_t orig_signal_mask, + tmp_signal_mask; + sigemptyset(&orig_signal_mask); + + /* Set up new mask to block all signals. */ + sigfillset(&tmp_signal_mask); + + /* Block all signals. */ + pthread_sigmask(SIG_BLOCK, &tmp_signal_mask, &orig_signal_mask); + if (!pdt->buffer.empty ()) { const std::string str = pdt->buffer.top()->str(); @@ -231,6 +248,9 @@ class NXLog /* Remove from stack. */ pdt->buffer.pop(); } + + /* Restore old signal mask. */ + pthread_sigmask(SIG_SETMASK, &orig_signal_mask, NULL); } -- cgit v1.2.3