Opened 5 years ago

Closed 5 years ago

#5558 closed bug (fixed)

Deadlock using unsafePerformIO to create a global MVar

Reported by: int-e Owned by: simonmar
Priority: high Milestone: 7.4.1
Component: Runtime System Version: 7.2.1
Keywords: Cc: jmg@…
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Incorrect result at runtime Test Case: concurrent/should_run/5558
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The following program occasionally terminates with a BlockedIndefinitelyOnMVar exception. This is unexpected. (According to SimonM,

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")
 ,("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")
 ,("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")
 ,("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.

Change History (5)

comment:1 Changed 5 years ago by jmg

Cc: jmg@… added

comment:2 Changed 5 years ago by jmg

I can reproduce this with 6.12.1, 7.0.3, and 7.2.1 on OS X Lion. Although it appears to work most of the time.

comment:3 Changed 5 years ago by simonmar

Milestone: 7.4.1
Owner: set to simonmar
Priority: normalhigh

I know what this is, working on a fix.

comment:4 Changed 5 years ago by marlowsd@…

commit 96c80d34163fd422cbc18f4532b7556212a554b8

Author: Simon Marlow <>
Date:   Mon Oct 17 13:16:02 2011 +0100

    make CAFs atomic, to fix #5558
    See Note [atomic CAFs] in rts/sm/Storage.c

 compiler/codeGen/CgClosure.lhs  |   25 +++----
 compiler/codeGen/CgUtils.hs     |   12 ++--
 compiler/codeGen/StgCmmBind.hs  |   31 ++++-----
 compiler/codeGen/StgCmmUtils.hs |   17 ++---
 includes/rts/storage/GC.h       |    4 +-
 rts/sm/Storage.c                |  143 +++++++++++++++++++++++++++++----------
 6 files changed, 150 insertions(+), 82 deletions(-)

comment:5 Changed 5 years ago by simonmar

Resolution: fixed
Status: newclosed
Test Case: concurrent/should_run/5558

test added

Note: See TracTickets for help on using tickets.