Every Haskell article about exceptions repeats simple pattern: Use bracket function to allocate/release resources and you will be safe.
I have a concern, because I tested its behavior and detected that the thread can get async exception while working inside release section.
import Control.Exception
import Control.Concurrent
main = do
tid <- forkIO myThread
threadDelay 100000
throwTo tid StackOverflow
threadDelay 1000000
myThread =
bracket
(putStrLn "NEW")
(\() -> threadDelay 500000 >> putStrLn "CLEAN")
(\() -> putStrLn "USE")
In the snippet above "CLEAN" is not printed due async exception - so resource leaks! How it can be claimed safe?
I don't know everything it must be a reason. To make it safe as I see it I need to wrap every cleaning inside mask. It looks clumsy.