Opened 6 years ago

Closed 6 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:

Description

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.

Change History (5)

comment:1 Changed 6 years ago by jmg

Cc: jmg@… added

comment:2 Changed 6 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 6 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 6 years ago by marlowsd@…

commit 96c80d34163fd422cbc18f4532b7556212a554b8

Author: Simon Marlow <marlowsd@gmail.com>
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 6 years ago by simonmar

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

test added

Note: See TracTickets for help on using tickets.