From 773dcaeca3410c9da05de68e66cf7296c0da8f52 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Thu, 10 Aug 2017 21:51:55 +0200 Subject: [PATCH] Fix a race condition in the monitor thread. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Startup log messages could be lost because the monitor would be started but not ready to process messages. Fix that by “warming up” the monitoring thread, having it execute a small computation and more importantly wait for the result to be received back, blocking. See #599 where parsing errors from a wrong URL were missed in the command line output, quite disturbingly. --- src/utils/monitor.lisp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/utils/monitor.lisp b/src/utils/monitor.lisp index 5b5d0c9..2e4b96c 100644 --- a/src/utils/monitor.lisp +++ b/src/utils/monitor.lisp @@ -31,7 +31,6 @@ (defstruct start start-logger) (defstruct stop stop-logger) (defstruct report-summary reset) -(defstruct noop) (defstruct log-message category description arguments) (defstruct new-label section label dbname) (defstruct update-stats section label read rows errs secs rs ws start stop) @@ -140,9 +139,16 @@ (setf *monitoring-kernel* kernel *monitoring-channel* (lp:make-channel)) + ;; warm up the channel to ensure we don't loose any event + (lp:submit-task *monitoring-channel* '+ 1 2 3) + (lp:receive-result *monitoring-channel*) + + ;; now that we know the channel is ready, start our long-running monitor (lp:submit-task *monitoring-channel* #'monitor *monitoring-queue*) (send-event (make-start :start-logger start-logger)) + (sleep 0.2) + *monitoring-channel*)) (defun stop-monitor (&key @@ -175,9 +181,7 @@ ;; process messages from the queue (loop :with start-time := (get-internal-real-time) - :for event := (multiple-value-bind (event available) - (lq:try-pop-queue queue) - (if available event (make-noop))) + :for event := (lq:pop-queue queue) :do (typecase event (start (when (start-start-logger event) @@ -201,9 +205,6 @@ :data (make-pgstate) :post (make-pgstate))))) - (noop - (sleep 0.2)) ; avoid buzy looping - (log-message ;; cl-log:log-message is a macro, we can't use apply ;; here, so we need to break a level of abstraction