Opened 7 years ago

Closed 7 years ago

#4876 closed bug (fixed)

isEmptySampleVar returns False when threads are waiting on an empty SampleVar

Reported by: guest Owned by: simonmar
Priority: high Milestone: 7.0.2
Component: libraries/base Version: 7.0.1
Keywords: Cc: eric.stansifer+haskell@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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
   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.

Change History (3)

comment:1 Changed 7 years ago by simonmar

Milestone: 7.0.2
Owner: set to simonmar
Priority: normalhigh

Looks like a straightforward bug, I'll take it.

comment:2 Changed 7 years ago by simonmar

Status: newmerge


Thu Jan  6 07:46:54 PST 2011  Simon Marlow <>
  * fix #4876

comment:3 Changed 7 years ago by igloo

Resolution: fixed
Status: mergeclosed


Note: See TracTickets for help on using tickets.