[Haskell-cafe] Brackets and Asynchronous Exceptions

Jason Dagit dagit at codersbase.com
Wed Apr 15 19:13:55 EDT 2009


On Wed, Apr 15, 2009 at 4:06 PM, Andrew Gallagher <ajcg at cs.ucla.edu> wrote:
> Hi,
>
> In a program I am writing, I have many locations where I acquire a resource,
> perform an operation with it, then release it.  I have been using the
> 'bracket' function so that the "release" operation would be performed even
> if the operation threw an exception.  This seems to work nicely.
>
> In the event of an asynchronous exception, however, is there a possible
> scenario where a release is not performed after an acquire?
>
> Looking at the example given in bracket documentation:
>
> bracket
>   (openFile "filename" ReadMode)
>   (hClose)
>   (\fileHandle -> do { ... })
>
> Is it possbile that an asynchronous exception could be raised in this thread
> after openFile executes but *before* the appropriate handlers are installed
> and the operation is run, preventing hClose from executing?
>
> If 'bracket' does not handle this case, should I be using the block/unblock
> functions to disable asynchronous exceptions:
>
> block
>   (bracket
>      (openFile "filename" ReadMode)
>      (hClose)
>      (\fileHandle -> do
>         unblock
>         ({ ... })))

Does this answer your question?
http://haskell.org/ghc/docs/latest/html/libraries/base/src/Control-Exception-Base.html#bracket

If so, I found it by going to haskell.org/hoogle searching for bracket
and then following the haddock "Source" to the definition.

I hope that helps!
Jason


More information about the Haskell-Cafe mailing list