[Haskell-cafe] BlockedIndefinitelyOnMVar

Massimo Zaniboni massimo.zaniboni at gmail.com
Fri Jul 13 14:51:06 UTC 2018


Hi,

I have a code like this

```
producer :: Int -> MVar Int -> IO ()
producer c mvar
   = case c > 10 of
       True -> throwIO HighNumberException
       False -> do putMVar mvar (c + 1)
                   producer (c + 1) mvar

consumer :: MVar Int -> IO ()
consumer mvar = do
   _ <- takeMVar mvar
   consumer mvar

coordinator :: MVar Int -> IO ()
coordinator mvar = do
   withAsync (producer 0 mvar) $ \p ->
     withAsync (consumer mvar) $ \c -> do
       wait c
       wait p

main :: IO ()
main = do
   mvar <- newEmptyMVar
   withAsync (coordinator mvar) (wait)
   putStrLn "done"
```

The producer send the informative exception HighNumberException, but the 
coordinator job receives a generic BlockedIndefinitelyOnMVar, because 
the producer stop working.

How can I discard the generic exception, and re-throw/catch only 
HighNumberException?

In case the complete source code is on 
https://github.com/massimo-zaniboni/blocked-on-mvar

Thanks in any case,
Massimo


More information about the Haskell-Cafe mailing list