Skip to content

`BlockedIndefinitelyOnMVar` is thrown to non-blocked threads

Summary

BlockedIndefinitelyOnMVar is thrown to other threads that weren't blocked.

Steps to reproduce

-- Test.hs

{-# LANGUAGE TypeApplications #-}

module Main (main) where

import Control.Concurrent
import Control.Exception

main :: IO ()
main = do
  emptyVar <- newEmptyMVar @()
  resultVar <- newEmptyMVar

  _ <- forkIO $ do
    caught <- try @SomeException (readMVar emptyVar)
    putMVar resultVar caught
    print ("thread", caught)

  result <- readMVar resultVar
  print ("main", result)
$ ghc -Wall -Wextra -threaded Test.hs
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
$ ./Test        
("thread",Left thread blocked indefinitely in an MVar operation)
Test: thread blocked indefinitely in an MVar operation
$ ./Test +RTS -N                     
Test: thread blocked indefinitely in an MVar operation
("thread",Left thread blocked indefinitely in an MVar operation)

It appears the main thread is being thrown a BlockedIndefinitelyOnMVar as well.

This can be decoupled from the empty MVar read:

-- Test.hs

{-# LANGUAGE TypeApplications #-}

module Main (main) where

import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception

main :: IO ()
main = do
  emptyVar <- newEmptyTMVarIO @()
  resultVar <- newEmptyMVar

  _ <- forkIO $ do
    caught <- try @SomeException (atomically (readTMVar emptyVar))
    putMVar resultVar caught
    print ("thread", caught)

  result <- readMVar resultVar
  print ("main", result)
$ ghc -Wall -Wextra -threaded Test.hs
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
$ ./Test 
("thread",Left thread blocked indefinitely in an STM transaction)
Test: thread blocked indefinitely in an MVar operation
$ ./Test +RTS -N
Test: thread blocked indefinitely in an MVar operation
("thread",Left thread blocked indefinitely in an STM transaction)

Expected behavior

I expect both threads to print the result of try in unspecified order.

 λ ./Test +RTS -N                              
("thread",Left thread blocked indefinitely in an MVar operation)
("main",Left thread blocked indefinitely in an MVar operation)

Environment

  • GHC version used:
    • 8.10.1
    • 8.8.3

Optional:

  • Operating System: NixOS, Linux 5.4.31 x86_64
Edited by Ole
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information