Is this a concurrency bug in base?

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Wed Oct 12 13:24:52 CEST 2011


Jean-Marie Gaillourdet wrote:
> This is my previous program with your workaround, it is also attached as TypeRepEqLock.hs
> 
[snip]
> Compile and execute:
> 
> $ ghc-7.0.3 -threaded -rtsopts TypeRepEqLock.hs
> <snip>
> $ while true ; do ./TypeRepEqLock +RTS -N ; done
> Ok
> Ok
> Ok
> Ok
> Ok
> Ok
> Ok
> Ok
> Ok
> TypeRepEqLock: thread blocked indefinitely in an MVar operation
> Ok
> Ok
> Ok
> ^C^C

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.

Best regards,

Bertram



More information about the Glasgow-haskell-users mailing list