@@ -60,7 +60,7 @@ gbracket_
6060 :: Monad m
6161 => m c -- ^ before
6262 -> (c -> m d) -- ^ after, on normal stop
63− -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
63+ -> (c -> e -> Stream m b -> m (Stream m b)) -- ^ on exception
6464 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
6565 -> (c -> Stream m b) -- ^ stream generator
6666 -> Stream m b
@@ -83,9 +83,9 @@ gbracket_ bef aft onExc ftry action =
8383 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
8484 Stop -> aft v >> return Stop
8585 -- XXX Do not handle async exceptions, just rethrow them.
86− Left e ->
87− return
88− $ Skip (GBracketException (onExc v e (UnStream step1 st)))
86+ Left e -> do
87+ strm <- onExc v e (UnStream step1 st)
88+ return $ Skip (GBracketException strm)
8989 step gst (GBracketException (UnStream step1 st)) = do
9090 res <- step1 gst st
9191 case res of
@@ -265,7 +265,7 @@ onException action stream =
265265 gbracket_
266266 (return ()) -- before
267267 return -- after
268− (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
268+ (\_ (e :: MC.SomeException) _ -> action >> MC.throwM e)
269269 (inline MC.try)
270270 (const stream)
271271
@@ -301,7 +301,7 @@ bracketUnsafe bef aft =
301301 gbracket_
302302 bef
303303 aft
304− (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
304+ (\a (e :: SomeException) _ -> aft a >> MC.throwM e)
305305 (inline MC.try)
306306
307307-- For a use case of this see the "streamly-process" package. It needs to kill
@@ -437,7 +437,7 @@ finallyIO action xs = bracketIO3 (return ()) act act act (const xs)
437437--
438438{-# INLINE_NORMAL ghandle #-}
439439ghandle :: (MonadCatch m, Exception e)
440− => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
440+ => (e -> Stream m a -> m (Stream m a)) -> Stream m a -> Stream m a
441441ghandle f stream =
442442 gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
443443
@@ -448,7 +448,7 @@ ghandle f stream =
448448--
449449{-# INLINE_NORMAL handle #-}
450450handle :: (MonadCatch m, Exception e)
451− => (e -> Stream m a) -> Stream m a -> Stream m a
451+ => (e -> m (Stream m a)) -> Stream m a -> Stream m a
452452handle f stream =
453453 gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
454454