Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created September 20, 2016 11:14
Show Gist options
  • Save snoyberg/c7e07b4fe2885fe93026a6716e066b61 to your computer and use it in GitHub Desktop.
Save snoyberg/c7e07b4fe2885fe93026a6716e066b61 to your computer and use it in GitHub Desktop.
Demonstrate a bug with fast-logger when concurrently writing chunks larger than the buffer size
-- Compiled with:
--
-- stack --resolver lts-6.17 ghc --package async --package fast-logger -- -threaded -rtsopts -with-rtsopts -N large-fast-logger.hs
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent.Async (Concurrently (..))
import Control.Monad (unless, zipWithM_)
import Data.Foldable (traverse_)
import Data.Monoid ((<>))
import Data.String (fromString)
import System.Log.FastLogger (LoggerSet, newFileLoggerSet, pushLogStr,
rmLoggerSet)
main :: IO ()
main = do
loggerSet <- newFileLoggerSet 16 "log.txt" -- really small on purpose
runConcurrently $ traverse_ (Concurrently . run loggerSet) ['A'..'Z']
rmLoggerSet loggerSet
output <- readFile "log.txt"
zipWithM_ checkLine [1..] (lines output)
run :: LoggerSet -> Char -> IO ()
run loggerSet c =
pushLogStr loggerSet str
where
str = fromString (replicate 99 c) <> "\n"
checkLine :: Int -> String -> IO ()
checkLine lineNumber chars = do
unless (length chars == 99)
$ putStrLn $ "Does not have 99 characters: " ++ show lineNumber
case chars of
[] -> return () -- already mentioned above
x:xs
| any (/= x) xs ->
putStrLn $ "Mismatched characters: " ++ show lineNumber
| otherwise -> return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment