Deadlock using unsafePerformIO to create a global MVar
The following program occasionally terminates with a BlockedIndefinitelyOnMVar exception. This is unexpected. (According to SimonM, http://www.haskell.org/pipermail/glasgow-haskell-users/2011-October/021084.html)
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 ()
I tested the program as follows:
> ghc --make -rtsopts -threaded Main.hs; while ./Main +RTS -N; do true; done
Main: thread blocked indefinitely in an MVar operation
I'm using ghc 7.2.1.
> ghc --info
[("Project name","The Glorious Glasgow Haskell Compilation System")
,("GCC extra via C opts"," -fwrapv")
,("C compiler command","/usr/bin/gcc")
,("C compiler flags"," -fno-stack-protector")
,("ar command","/usr/bin/ar")
,("ar flags","q")
,("ar supports at file","YES")
,("touch command","touch")
,("dllwrap command","/bin/false")
,("windres command","/bin/false")
,("perl command","/usr/bin/perl")
,("Project version","7.2.1")
,("Booter version","7.0.3")
,("Stage","2")
,("Build platform","x86_64-unknown-linux")
,("Host platform","x86_64-unknown-linux")
,("Target platform","x86_64-unknown-linux")
,("Have interpreter","YES")
,("Object splitting supported","YES")
,("Have native code generator","YES")
,("Support SMP","YES")
,("Unregisterised","NO")
,("Tables next to code","YES")
,("RTS ways","l debug thr thr_debug thr_l thr_p dyn debug_dyn thr_dyn thr_debug_dyn")
,("Leading underscore","NO")
,("Debug on","False")
,("LibDir","/opt/ghc-7.2.1/lib/ghc-7.2.1")
,("Global Package DB","/opt/ghc-7.2.1/lib/ghc-7.2.1/package.conf.d")
,("Gcc Linker flags","[]")
,("Ld Linker flags","[]")
]
/proc/cpuinfo lists 4 Intel(R) Core(TM) i7 CPU M 620 @ 2.67GHz cores.
Trac metadata
| Trac field | Value |
|---|---|
| Version | 7.2.1 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Runtime System |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture |