[Haskell-cafe] Asynchronous exception wormholes kill modularity

Bas van Dijk v.dijk.bas at gmail.com
Fri Mar 26 11:37:01 EDT 2010

On Fri, Mar 26, 2010 at 3:43 PM, Gregory Collins
<greg at gregorycollins.net> wrote:
> Matthew Brecknell <matthew at brecknell.net> writes:
>> And is confirmed by a simple test (with GHC 6.10.4 on Linux):
>> import Prelude hiding(catch)
>> import Control.Concurrent
>> import Control.Exception
>> main = do
>>   chan <- newEmptyMVar
>>   done <- newEmptyMVar
>>   kill <- block $ forkIO $ do
>>     (takeMVar chan >>= putMVar done)
>>       `onException` putMVar done "Exception received during takeMVar"
>> ...
> Should this be "forkIO $ block" instead of "block $ forkIO"?


First of all note that forked threads inherit the blocked state of
their parents:


The reason Matthew first blocks then calls forkIO is that he needs to
install an exception handler for the forked thread:

  `onException` putMVar done "Exception received during takeMVar"

If he does not call block or only later calls it in the forked thread
a chance exists that an asynchronous exception is thrown to the forked
thread before the exception handler is installed.



More information about the Haskell-Cafe mailing list