Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,261
    • Issues 5,261
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 567
    • Merge requests 567
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #5558
Closed
Open
Issue created Oct 14, 2011 by Bertram Felgenhauer@int-eReporter

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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking