Skip to content

Commit

Permalink
Handle async exceptions correctly in bracket and finally.
Browse files Browse the repository at this point in the history
  • Loading branch information
dterei committed Jul 10, 2014
1 parent 0e12431 commit 6024297
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions simple/src/Web/Simple/Controller/Exception.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ onException act handler = control $ \runInM -> do
runInM act `E.onException` runInM handler

finally :: Controller s a -> Controller s b -> Controller s a
finally act handler = do
res <- (act `onException` handler)
handler
return res
finally act next = control $ \runInM -> E.mask $ \restore -> do
r <- restore (runInM act) `E.onException` (runInM next)
_ <- runInM next
return r

bracket :: Controller s a -> (a -> Controller s b)
-> (a -> Controller s c) -> Controller s c
bracket aquire release act = do
a <- aquire
act a `finally` release a
bracket aquire release act = control $ \runInM -> E.mask $ \restore -> do
let release' a = runInM $ restoreM a >>= release
a <- runInM aquire
r <- (restore $ runInM $ restoreM a >>= act) `E.onException` release' a
_ <- release' a
return r

handle :: E.Exception e => (e -> Controller s a) -> Controller s a
-> Controller s a
Expand Down

0 comments on commit 6024297

Please sign in to comment.