From 57ad67ed1b1a176e332f6473e4405b7792fa6c2c Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Fri, 3 May 2024 09:49:08 -0400 Subject: [PATCH] Delay for 0.1s when flushing fast-logger has a bug[^1] where flushing immediately after pushing a log message sometimes does not find anything to flush. Typically, users will flush the log because they intend to print non-logging output or as part of program exit. This bug causes output to be interleaved incorrectly in the former case and log messages to be lost entirely in the latter. To work around this, we delay 0.1s before flushing. There's no guarantee this works, and I don't know if the delay could be shorter or should be longer, but it has made things work reliably in my testing so far. [^1]: https://github.com/kazu-yamamoto/logger/issues/213 --- src/Blammo/Logging/Logger.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Blammo/Logging/Logger.hs b/src/Blammo/Logging/Logger.hs index 89bc163..bda5faa 100644 --- a/src/Blammo/Logging/Logger.hs +++ b/src/Blammo/Logging/Logger.hs @@ -27,6 +27,7 @@ import Blammo.Logging.LogSettings import Blammo.Logging.Terminal import Blammo.Logging.Test hiding (getLoggedMessages) import qualified Blammo.Logging.Test as LoggedMessages +import Control.Concurrent (threadDelay) import Control.Lens (view) import Control.Monad (unless) import Control.Monad.IO.Class (MonadIO (..)) @@ -82,7 +83,11 @@ pushLogStrLn logger str = case lLoggedMessages logger of flushLogStr :: MonadIO m => Logger -> m () flushLogStr logger = case lLoggedMessages logger of - Nothing -> liftIO $ FastLogger.flushLogStr loggerSet + Nothing -> liftIO $ do + -- Delay for 0.1s before flushing to work around + -- https://github.com/kazu-yamamoto/logger/issues/213 + threadDelay 100000 + FastLogger.flushLogStr loggerSet Just _ -> pure () where loggerSet = getLoggerLoggerSet logger