Skip to content
This repository
Browse code

Make protect safer against exceptions in a Pipe value.

Fix an issue reported by Michael Snoyman, where an exception contained
in a `Pipe` value would cause finalizers to be leaked.

This commit introduces an extra monadic step at each `protect` call,
which ensures that any exceptions contained in the continuation thunk
can be captured and rethrown within the `Pipe` monad.
  • Loading branch information...
commit 7bb846ea620f1a2595c45cb4a52b16ff3fe1febd 1 parent 8e09373
Paolo Capriotti authored June 12, 2012

Showing 1 changed file with 1 addition and 1 deletion. Show diff stats Hide diff stats

  1. 2  Control/Pipe/Common.hs
2  Control/Pipe/Common.hs
@@ -200,7 +200,7 @@ discard :: Monad m => Pipe a b m r
200 200
 discard = forever await
201 201
 
202 202
 protect :: Monad m => Finalizer m -> Pipe a b m r -> Pipe a b m r
203  
-protect w = go
  203
+protect w p = M Masked (return $! go p) (`Throw` w)
204 204
   where
205 205
     go (Pure r w') = Pure r (w ++ w')
206 206
     go (Throw e w') = Throw e (w ++ w')

0 notes on commit 7bb846e

Please sign in to comment.
Something went wrong with that request. Please try again.