Opened 3 years ago

Closed 3 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 Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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.

Change History (3)

comment:1 Changed 3 years ago by simonmar

  • Milestone set to 7.0.2
  • Owner set to simonmar
  • Priority changed from normal to high

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

comment:2 Changed 3 years ago by simonmar

  • Status changed from new to merge

Fixed:

Thu Jan  6 07:46:54 PST 2011  Simon Marlow <marlowsd@gmail.com>
  * fix #4876

comment:3 Changed 3 years ago by igloo

  • Resolution set to fixed
  • Status changed from merge to closed

Merged.

Note: See TracTickets for help on using tickets.