Weird behavior of the NonTermination exception
Bas van Dijk
v.dijk.bas at gmail.com
Thu May 3 18:14:44 CEST 2012
On 3 May 2012 17:31, Edward Z. Yang <ezyang at mit.edu> wrote:
> Excerpts from Bas van Dijk's message of Thu May 03 11:10:38 -0400 2012:
>> As can be seen, the putMVar is executed successfully. So why do I get
>> the message: "thread blocked indefinitely in an MVar operation"?
>
> GHC will send BlockedIndefinitelyOnMVar to all threads involved
> in the deadlock, so it's not unusual that this can interact with
> error handlers to cause the system to become undeadlocked.
But why is the BlockedIndefinitelyOnMVar thrown in the first place?
According to the its documentation and your very enlightening article
it is thrown when:
"The thread is blocked on an MVar, but there are no other references
to the MVar so it can't ever continue."
The first condition holds for the main thread since it's executing
takeMVar. But the second condition doesn't hold since the forked
thread still has a reference to the MVar.
I just tried delaying the thread before the putMVar:
-------------------------------------------------
main :: IO ()
main = do
mv <- newEmptyMVar
_ <- forkIO $ do
catch action
(\e -> putStrLn $ "I solved the Halting Problem: " ++
show (e :: SomeException))
putStrLn "Delaying for 2 seconds..."
threadDelay 2000000
putStrLn "putting MVar..."
putMVar mv ()
putStrLn "putted MVar"
takeMVar mv
-------------------------------------------------
Now I get the following output:
loop: thread blocked indefinitely in an MVar operation
I solved the Halting Problem: <<loop>>
Delaying for 2 seconds...
Now it seems the thread is killed while delaying. But why is it
killed? It could be a BlockedIndefinitelyOnMVar that is thrown.
However I get the same output when I catch and print all exceptions in
the forked thread:
main :: IO ()
main = do
mv <- newEmptyMVar
_ <- forkIO $
handle (\e -> putStrLn $ "Oh nooo:" ++
show (e :: SomeException)) $ do
catch action
(\e -> putStrLn $ "I solved the Halting Problem: " ++
show (e :: SomeException))
putStrLn "Delaying for 2 seconds..."
threadDelay 2000000
putStrLn "putting MVar..."
putMVar mv ()
putStrLn "putted MVar"
takeMVar mv
Bas
More information about the Glasgow-haskell-users
mailing list