input-output-hk / io-sim

Haskell's IO simulator which closely follows core packages (base, async, stm).
https://hackage.haskell.org/package/io-sim
Apache License 2.0
37 stars 15 forks source link

flushTQueue is broken #133

Closed ch1bo closed 8 months ago

ch1bo commented 8 months ago

Describe the bug flushTQueue in IOSim s does not empty the queue, while it does in IO

To Reproduce

Execute this program:

#!/usr/bin/env cabal
{- cabal:
build-depends: base, io-sim ^>= 1.3, io-classes ^>= 1.3
-}

import Control.Concurrent.Class.MonadSTM
import Control.Exception (assert)
import Control.Monad (unless)
import Control.Monad.IOSim

main :: IO ()
main = do
  emptyInIO <- emptyQueueAfterFlush
  unless emptyInIO $
    error "queue not emptied from IO"

  let emptyInIOSim = runSimOrThrow emptyQueueAfterFlush
  unless emptyInIOSim $
    error "queue not emptied from IOSim"

emptyQueueAfterFlush :: MonadSTM m => m Bool
emptyQueueAfterFlush = do
  q <- newTQueueIO
  atomically $ do
    writeTQueue q 1
    _ <- flushTQueue q
    isEmptyTQueue q

Expected behaviour Same semantics of STM in IO and IOSim

Additional context The flushQueueDefault looks wrong when compared with the one of io-classes and the real one from stm