Is this a concurrency bug in base?

Jean-Marie Gaillourdet jmg at gaillourdet.net
Wed Oct 12 13:54:24 CEST 2011


Hi Bertram,

On 12.10.2011, at 13:24, Bertram Felgenhauer wrote:

> This has nothing to do with Data.Typeable though - it appears to be some
> interaction between unsaferPerformIO and MVars that I do not understand.
> The following program occasionally terminates with "thread blocked
> indefinitely in an MVar operation", too (tested on ghc 7.0.3 and 7.2.1):
> 
> import Control.Concurrent
> import Control.Exception
> import Control.Monad
> import System.IO.Unsafe
> 
> main :: IO ()
> main = do
>    -- evaluate lock -- adding this line fixes the problem
> 
>    fin1 <- newEmptyMVar
>    fin2 <- newEmptyMVar
> 
>    forkIO $ ping >>= putMVar fin1
>    forkIO $ ping >>= putMVar fin2
> 
>    takeMVar fin1
>    takeMVar fin2
> 
> {-# NOINLINE lock #-}
> lock :: MVar ()
> lock = unsafePerformIO $ newMVar ()
> 
> ping = do
>    () <- takeMVar lock
>    putMVar lock ()
> 
> Since I don't yet understand why this blocks, I cannot say whether it
> should work or not.

I've seen blocks with GHC 6.12.1, but I never got one with 7.0.3 and 7.2.1. Probably, bad luck on my side. ;-)

Cheers,
  Jean




More information about the Glasgow-haskell-users mailing list