Skip to content

isEmptySampleVar returns False when threads are waiting on an empty SampleVar

The source code for Control.Concurrent.SampleVar.isEmptySampleVar:

isEmptySampleVar :: SampleVar a -> IO Bool
isEmptySampleVar (SampleVar svar) = do
   (readers, _) <- readMVar svar
   return (readers == 0)

should have readers <= 0, as the state readers < 0 is used to indicate the SampleVar is empty and has threads waiting on it.

As an example:

import System.Random
import Control.Concurrent.SampleVar
import Control.Concurrent

do_something = threadDelay 100000     -- 100 ms
loop body = body >> loop body

produce, consume :: SampleVar Int -> IO ()
produce svar = do
   do_something
   b <- isEmptySampleVar svar
   if b then randomIO >>= writeSampleVar svar else return ()

consume svar = readSampleVar svar >>= print

main = do
   svar <- newEmptySampleVar
   forkIO $ loop $ produce svar
   forkIO $ loop $ consume svar
   threadDelay 1000000       -- one second

This code deadlocks instead of printing random numbers.

Trac metadata
Trac field Value
Version 7.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC eric.stansifer+haskell@gmail.com
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information