[Haskell-cafe] Brackets and Asynchronous Exceptions

Andrew Gallagher ajcg at CS.UCLA.EDU
Wed Apr 15 19:06:45 EDT 2009


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
          ({ ... })))

Thanks,
Andrew


More information about the Haskell-Cafe mailing list