on master help sign in
Make the finalizer based exception handling routines the default.

- Change the handlers for both streams and unfolds.

commit960def155f6e300c6789b332f44dd3c2fb91b163 authorPranay Sashank <pranaysashank@composewell.com> dateTue 18 Aug 2020, 15:20 (6 years ago) parent1944ba0
8 files changed+183−169
Changelog.md+6 benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+54−54 src/Streamly/Internal/Data/Stream/StreamD.hs+43−35 src/Streamly/Internal/Data/Unfold.hs+38−38 src/Streamly/Internal/FileSystem/File.hs+10−10 src/Streamly/Internal/Network/Inet/TCP.hs+4−4 src/Streamly/Internal/Network/Socket.hs+1−1 src/Streamly/Internal/Prelude.hs+27−27
Changelog.md+6
@@ -6,6 +6,10 @@
66 accumulator monadic.
77* Change the signature of `concatMapWith` to ensure that it can be
88 used with a wide variety of combining functions.
9+* Exception handling functions `bracket`, `handle`, `finally` now
10+ require an additional `MonadAsync` constraint. Several other
11+ functions that used these functions also now require the additional
12+ constraint.
913* Change the associativity of combinators `serial`, `wSerial`,
1014 `ahead`, `async`, `wAsync`, `parallel` to be the same as `<>`.
1115* Drop support for GHC 7.10.3.
@@ -15,6 +19,8 @@
1519* `accept*` and `connect` APIs in `Streamly.Network.Inet.TCP` and the `accept`
1620 API in `Streamly.Network.Socket` now close the socket if an exception is
1721 thrown.
22+* `bracket`, `handle`, and `finally` now work on streams that aren't
23+ fully drained.
1824
1925## 0.7.2
2026
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+54−54
@@ -110,19 +110,19 @@ inspect $ hasNoTypeClasses 'readChunksOnException
110110#endif
111111
112112-- | Send the file contents to /dev/null with exception handling
113−readChunksBracket :: Handle -> Handle -> IO ()
114−readChunksBracket inh devNull =
115− let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
113+readChunksBracket_ :: Handle -> Handle -> IO ()
114+readChunksBracket_ inh devNull =
115+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.readChunks
116116 in IUF.fold readEx (IFH.writeChunks devNull) inh
117117
118118#ifdef INSPECTION
119−inspect $ hasNoTypeClasses 'readChunksBracket
119+inspect $ hasNoTypeClasses 'readChunksBracket_
120120-- inspect $ 'readChunksBracket `hasNoType` ''Step
121121#endif
122122
123−readChunksBracketIO :: Handle -> Handle -> IO ()
124−readChunksBracketIO inh devNull =
125− let readEx = IUF.bracketIO return (\_ -> hClose inh) FH.readChunks
123+readChunksBracket :: Handle -> Handle -> IO ()
124+readChunksBracket inh devNull =
125+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
126126 in IUF.fold readEx (IFH.writeChunks devNull) inh
127127
128128o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
@@ -130,10 +130,10 @@ o_1_space_copy_exceptions_readChunks env =
130130 [ bgroup "copy/readChunks/exceptions"
131131 [ mkBench "UF.onException" env $ \inH _ ->
132132 readChunksOnException inH (nullH env)
133+ , mkBench "UF.bracket_" env $ \inH _ ->
134+ readChunksBracket_ inH (nullH env)
133135 , mkBench "UF.bracket" env $ \inH _ ->
134136 readChunksBracket inH (nullH env)
135− , mkBench "UF.bracketIO" env $ \inH _ ->
136− readChunksBracketIO inH (nullH env)
137137 ]
138138 ]
139139
@@ -142,22 +142,22 @@ o_1_space_copy_exceptions_readChunks env =
142142-------------------------------------------------------------------------------
143143
144144-- | Send the file contents to /dev/null with exception handling
145−toChunksBracket :: Handle -> Handle -> IO ()
146−toChunksBracket inh devNull =
147− let readEx = S.bracket
145+toChunksBracket_ :: Handle -> Handle -> IO ()
146+toChunksBracket_ inh devNull =
147+ let readEx = IP.bracket_
148148 (return ())
149149 (\_ -> hClose inh)
150150 (\_ -> IFH.toChunks inh)
151151 in S.fold (IFH.writeChunks devNull) $ readEx
152152
153153#ifdef INSPECTION
154−inspect $ hasNoTypeClasses 'toChunksBracket
154+inspect $ hasNoTypeClasses 'toChunksBracket_
155155-- inspect $ 'toChunksBracket `hasNoType` ''Step
156156#endif
157157
158−toChunksBracketIO :: Handle -> Handle -> IO ()
159−toChunksBracketIO inh devNull =
160− let readEx = IP.bracketIO
158+toChunksBracket :: Handle -> Handle -> IO ()
159+toChunksBracket inh devNull =
160+ let readEx = S.bracket
161161 (return ())
162162 (\_ -> hClose inh)
163163 (\_ -> IFH.toChunks inh)
@@ -166,10 +166,10 @@ toChunksBracketIO inh devNull =
166166o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
167167o_1_space_copy_exceptions_toChunks env =
168168 [ bgroup "copy/toChunks/exceptions"
169− [ mkBench "S.bracket" env $ \inH _ ->
169+ [ mkBench "S.bracket_" env $ \inH _ ->
170+ toChunksBracket_ inH (nullH env)
171+ , mkBench "S.bracket" env $ \inH _ ->
170172 toChunksBracket inH (nullH env)
171− , mkBench "S.bracketIO" env $ \inH _ ->
172− toChunksBracketIO inH (nullH env)
173173 ]
174174 ]
175175
@@ -308,35 +308,35 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionUnfold
308308#endif
309309
310310-- | Send the file contents to /dev/null with exception handling
311−readWriteFinallyUnfold :: Handle -> Handle -> IO ()
312−readWriteFinallyUnfold inh devNull =
313− let readEx = IUF.finally (\_ -> hClose inh) FH.read
311+readWriteFinally_Unfold :: Handle -> Handle -> IO ()
312+readWriteFinally_Unfold inh devNull =
313+ let readEx = IUF.finally_ (\_ -> hClose inh) FH.read
314314 in S.fold (FH.write devNull) $ S.unfold readEx inh
315315
316316#ifdef INSPECTION
317−inspect $ hasNoTypeClasses 'readWriteFinallyUnfold
317+inspect $ hasNoTypeClasses 'readWriteFinally_Unfold
318318-- inspect $ 'readWriteFinallyUnfold `hasNoType` ''Step
319319#endif
320320
321−readWriteFinallyIOUnfold :: Handle -> Handle -> IO ()
322−readWriteFinallyIOUnfold inh devNull =
323− let readEx = IUF.finallyIO (\_ -> hClose inh) FH.read
321+readWriteFinallyUnfold :: Handle -> Handle -> IO ()
322+readWriteFinallyUnfold inh devNull =
323+ let readEx = IUF.finally (\_ -> hClose inh) FH.read
324324 in S.fold (FH.write devNull) $ S.unfold readEx inh
325325
326326-- | Send the file contents to /dev/null with exception handling
327−readWriteBracketUnfold :: Handle -> Handle -> IO ()
328−readWriteBracketUnfold inh devNull =
329− let readEx = IUF.bracket return (\_ -> hClose inh) FH.read
327+readWriteBracket_Unfold :: Handle -> Handle -> IO ()
328+readWriteBracket_Unfold inh devNull =
329+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.read
330330 in S.fold (FH.write devNull) $ S.unfold readEx inh
331331
332332#ifdef INSPECTION
333−inspect $ hasNoTypeClasses 'readWriteBracketUnfold
333+inspect $ hasNoTypeClasses 'readWriteBracket_Unfold
334334-- inspect $ 'readWriteBracketUnfold `hasNoType` ''Step
335335#endif
336336
337−readWriteBracketIOUnfold :: Handle -> Handle -> IO ()
338−readWriteBracketIOUnfold inh devNull =
339− let readEx = IUF.bracketIO return (\_ -> hClose inh) FH.read
337+readWriteBracketUnfold :: Handle -> Handle -> IO ()
338+readWriteBracketUnfold inh devNull =
339+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.read
340340 in S.fold (FH.write devNull) $ S.unfold readEx inh
341341
342342o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
@@ -346,14 +346,14 @@ o_1_space_copy_read_exceptions env =
346346 readWriteOnExceptionUnfold inh (nullH env)
347347 , mkBenchSmall "UF.handle" env $ \inh _ ->
348348 readWriteHandleExceptionUnfold inh (nullH env)
349+ , mkBenchSmall "UF.finally_" env $ \inh _ ->
350+ readWriteFinally_Unfold inh (nullH env)
349351 , mkBenchSmall "UF.finally" env $ \inh _ ->
350352 readWriteFinallyUnfold inh (nullH env)
351− , mkBenchSmall "UF.finallyIO" env $ \inh _ ->
352− readWriteFinallyIOUnfold inh (nullH env)
353+ , mkBenchSmall "UF.bracket_" env $ \inh _ ->
354+ readWriteBracket_Unfold inh (nullH env)
353355 , mkBenchSmall "UF.bracket" env $ \inh _ ->
354356 readWriteBracketUnfold inh (nullH env)
355− , mkBenchSmall "UF.bracketIO" env $ \inh _ ->
356− readWriteBracketIOUnfold inh (nullH env)
357357 ]
358358 ]
359359
@@ -375,31 +375,31 @@ readWriteHandleExceptionStream inh devNull =
375375 in S.fold (FH.write devNull) $ readEx
376376
377377-- | Send the file contents to /dev/null with exception handling
378+readWriteFinally_Stream :: Handle -> Handle -> IO ()
379+readWriteFinally_Stream inh devNull =
380+ let readEx = IP.finally_ (hClose inh) (S.unfold FH.read inh)
381+ in S.fold (FH.write devNull) readEx
382+
378383readWriteFinallyStream :: Handle -> Handle -> IO ()
379384readWriteFinallyStream inh devNull =
380385 let readEx = S.finally (hClose inh) (S.unfold FH.read inh)
381386 in S.fold (FH.write devNull) readEx
382387
383−readWriteFinallyIOStream :: Handle -> Handle -> IO ()
384−readWriteFinallyIOStream inh devNull =
385− let readEx = IP.finallyIO (hClose inh) (S.unfold FH.read inh)
386− in S.fold (FH.write devNull) readEx
387−
388388-- | Send the file contents to /dev/null with exception handling
389−fromToBytesBracketStream :: Handle -> Handle -> IO ()
390−fromToBytesBracketStream inh devNull =
391− let readEx = S.bracket (return ()) (\_ -> hClose inh)
389+fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
390+fromToBytesBracket_Stream inh devNull =
391+ let readEx = IP.bracket_ (return ()) (\_ -> hClose inh)
392392 (\_ -> IFH.toBytes inh)
393393 in IFH.fromBytes devNull $ readEx
394394
395395#ifdef INSPECTION
396−inspect $ hasNoTypeClasses 'fromToBytesBracketStream
396+inspect $ hasNoTypeClasses 'fromToBytesBracket_Stream
397397-- inspect $ 'fromToBytesBracketStream `hasNoType` ''Step
398398#endif
399399
400−fromToBytesBracketIOStream :: Handle -> Handle -> IO ()
401−fromToBytesBracketIOStream inh devNull =
402− let readEx = IP.bracketIO (return ()) (\_ -> hClose inh)
400+fromToBytesBracketStream :: Handle -> Handle -> IO ()
401+fromToBytesBracketStream inh devNull =
402+ let readEx = S.bracket (return ()) (\_ -> hClose inh)
403403 (\_ -> IFH.toBytes inh)
404404 in IFH.fromBytes devNull $ readEx
405405
@@ -410,16 +410,16 @@ o_1_space_copy_stream_exceptions env =
410410 readWriteOnExceptionStream inh (nullH env)
411411 , mkBenchSmall "S.handle" env $ \inh _ ->
412412 readWriteHandleExceptionStream inh (nullH env)
413+ , mkBenchSmall "S.finally_" env $ \inh _ ->
414+ readWriteFinally_Stream inh (nullH env)
413415 , mkBenchSmall "S.finally" env $ \inh _ ->
414416 readWriteFinallyStream inh (nullH env)
415− , mkBenchSmall "S.finallyIO" env $ \inh _ ->
416− readWriteFinallyIOStream inh (nullH env)
417417 ]
418418 , bgroup "copy/fromToBytes/exceptions"
419− [ mkBenchSmall "S.bracket" env $ \inh _ ->
419+ [ mkBenchSmall "S.bracket_" env $ \inh _ ->
420+ fromToBytesBracket_Stream inh (nullH env)
421+ , mkBenchSmall "S.bracket" env $ \inh _ ->
420422 fromToBytesBracketStream inh (nullH env)
421− , mkBenchSmall "S.bracketIO" env $ \inh _ ->
422− fromToBytesBracketIOStream inh (nullH env)
423423 ]
424424 ]
425425
src/Streamly/Internal/Data/Stream/StreamD.hs+43−35
@@ -282,15 +282,16 @@ module Streamly.Internal.Data.Stream.StreamD
282282 , newFinalizedIORef
283283 , runIORefFinalizer
284284 , clearIORefFinalizer
285+ , gbracket_
285286 , gbracket
286287 , before
288+ , after_
287289 , after
288− , afterIO
290+ , bracket_
289291 , bracket
290− , bracketIO
291292 , onException
293+ , finally_
292294 , finally
293− , finallyIO
294295 , handle
295296
296297 -- * Concurrent Application
@@ -3131,8 +3132,8 @@ data GbracketState s1 s2 v
31313132--
31323133-- /Internal/
31333134--
3134−{-# INLINE_NORMAL gbracket #-}
3135−gbracket
3135+{-# INLINE_NORMAL gbracket_ #-}
3136+gbracket_
31363137 :: Monad m
31373138 => m c -- ^ before
31383139 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -3140,7 +3141,7 @@ gbracket
31403141 -> (c -> e -> Stream m b) -- ^ on exception
31413142 -> (c -> Stream m b) -- ^ stream generator
31423143 -> Stream m b
3143−gbracket bef exc aft fexc fnormal =
3144+gbracket_ bef exc aft fexc fnormal =
31443145 Stream step GBracketInit
31453146
31463147 where
@@ -3204,18 +3205,17 @@ data GbracketIOState s1 s2 v wref
32043205 | GBracketIONormal s1 v wref
32053206 | GBracketIOException s2
32063207
3207−-- | Like gbracket but also uses a finalizer to make sure when the stream is
3208−-- garbage collected we run the finalizing action. This requires a MonadIO and
3209−-- MonadBaseControl IO constraint.
3210−--
32113208-- | The most general bracketing and exception combinator. All other
32123209-- combinators can be expressed in terms of this combinator. This can also be
32133210-- used for cases which are not covered by the standard combinators.
32143211--
3212+-- | It uses a finalizer to make sure we run the finalizing action
3213+-- when the stream is garbage collected.
3214+--
32153215-- /Internal/
32163216--
3217−{-# INLINE_NORMAL gbracketIO #-}
3218−gbracketIO
3217+{-# INLINE_NORMAL gbracket #-}
3218+gbracket
32193219 :: (MonadIO m, MonadBaseControl IO m)
32203220 => m c -- ^ before
32213221 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -3223,7 +3223,7 @@ gbracketIO
32233223 -> (c -> e -> Stream m b) -- ^ on exception
32243224 -> (c -> Stream m b) -- ^ stream generator
32253225 -> Stream m b
3226−gbracketIO bef exc aft fexc fnormal =
3226+gbracket bef exc aft fexc fnormal =
32273227 Stream step GBracketIOInit
32283228
32293229 where
@@ -3283,9 +3283,9 @@ before action (Stream step state) = Stream step' Nothing
32833283 Stop -> return Stop
32843284
32853285-- | Run a side effect whenever the stream stops normally.
3286−{-# INLINE_NORMAL after #-}
3287−after :: Monad m => m b -> Stream m a -> Stream m a
3288−after action (Stream step state) = Stream step' state
3286+{-# INLINE_NORMAL after_ #-}
3287+after_ :: Monad m => m b -> Stream m a -> Stream m a
3288+after_ action (Stream step state) = Stream step' state
32893289
32903290 where
32913291
@@ -3297,10 +3297,10 @@ after action (Stream step state) = Stream step' state
32973297 Skip s -> return $ Skip s
32983298 Stop -> action >> return Stop
32993299
3300−{-# INLINE_NORMAL afterIO #-}
3301−afterIO :: (MonadIO m, MonadBaseControl IO m)
3300+{-# INLINE_NORMAL after #-}
3301+after :: (MonadIO m, MonadBaseControl IO m)
33023302 => m b -> Stream m a -> Stream m a
3303−afterIO action (Stream step state) = Stream step' Nothing
3303+after action (Stream step state) = Stream step' Nothing
33043304
33053305 where
33063306
@@ -3331,7 +3331,7 @@ afterIO action (Stream step state) = Stream step' Nothing
33313331{-# INLINE_NORMAL onException #-}
33323332onException :: MonadCatch m => m b -> Stream m a -> Stream m a
33333333onException action str =
3334− gbracket (return ()) MC.try return
3334+ gbracket_ (return ()) MC.try return
33353335 (\_ (e :: MC.SomeException) -> nilM (action >> MC.throwM e))
33363336 (\_ -> str)
33373337
@@ -3357,17 +3357,22 @@ _onException action (Stream step state) = Stream step' state
33573357-- generate a stream using the output, run the second action providing the
33583358-- remembered value as an argument whenever the stream ends normally or due to
33593359-- an exception.
3360−{-# INLINE_NORMAL bracket #-}
3361−bracket :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3362−bracket bef aft bet =
3363− gbracket bef MC.try aft
3360+{-# INLINE_NORMAL bracket_ #-}
3361+bracket_ :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3362+bracket_ bef aft bet =
3363+ gbracket_ bef MC.try aft
33643364 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
33653365
3366−{-# INLINE_NORMAL bracketIO #-}
3367−bracketIO :: (MonadAsync m, MonadCatch m)
3366+-- | Run the first action before the stream starts and remember its output,
3367+-- generate a stream using the output, run the second action providing the
3368+-- remembered value as an argument whenever the stream ends normally or due to
3369+-- an exception or gets garbage collected.
3370+--
3371+{-# INLINE_NORMAL bracket #-}
3372+bracket :: (MonadAsync m, MonadCatch m)
33683373 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3369−bracketIO bef aft bet =
3370− gbracketIO bef MC.try aft
3374+bracket bef aft bet =
3375+ gbracket bef MC.try aft
33713376 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
33723377
33733378data BracketState s v = BracketInit | BracketRun s v
@@ -3395,14 +3400,17 @@ _bracket bef aft bet = Stream step' BracketInit
33953400
33963401-- | Run a side effect whenever the stream stops normally or aborts due to an
33973402-- exception.
3398−{-# INLINE finally #-}
3399−finally :: MonadCatch m => m b -> Stream m a -> Stream m a
3403+{-# INLINE finally_ #-}
3404+finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
34003405-- finally action xs = after action $ onException action xs
3401−finally action xs = bracket (return ()) (\_ -> action) (const xs)
3406+finally_ action xs = bracket_ (return ()) (\_ -> action) (const xs)
34023407
3403−{-# INLINE finallyIO #-}
3404−finallyIO :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3405−finallyIO action xs = bracketIO (return ()) (\_ -> action) (const xs)
3408+-- | Run a side effect whenever the stream stops normally or aborts due to an
3409+-- exception or gets garbage collected.
3410+--
3411+{-# INLINE finally #-}
3412+finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3413+finally action xs = bracket (return ()) (\_ -> action) (const xs)
34063414
34073415-- | When evaluating a stream if an exception occurs, stream evaluation aborts
34083416-- and the specified exception handler is run with the exception as argument.
@@ -3410,7 +3418,7 @@ finallyIO action xs = bracketIO (return ()) (\_ -> action) (const xs)
34103418handle :: (MonadCatch m, Exception e)
34113419 => (e -> Stream m a) -> Stream m a -> Stream m a
34123420handle f str =
3413− gbracket (return ()) MC.try return (\_ e -> f e) (\_ -> str)
3421+ gbracket_ (return ()) MC.try return (\_ e -> f e) (\_ -> str)
34143422
34153423{-# INLINE_NORMAL _handle #-}
34163424_handle :: (MonadCatch m, Exception e)
src/Streamly/Internal/Data/Unfold.hs+38−38
@@ -111,16 +111,16 @@ module Streamly.Internal.Data.Unfold
111111 , apDiscardSnd
112112
113113 -- * Exceptions
114+ , gbracket_
114115 , gbracket
115− , gbracketIO
116116 , before
117+ , after_
117118 , after
118− , afterIO
119119 , onException
120+ , finally_
120121 , finally
121− , finallyIO
122+ , bracket_
122123 , bracket
123− , bracketIO
124124 , handle
125125 )
126126where
@@ -754,8 +754,8 @@ concatMapM f (Unfold step1 inject1) = Unfold step inject
754754--
755755-- /Internal/
756756--
757−{-# INLINE_NORMAL gbracket #-}
758−gbracket
757+{-# INLINE_NORMAL gbracket_ #-}
758+gbracket_
759759 :: Monad m
760760 => (a -> m c) -- ^ before
761761 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -763,7 +763,7 @@ gbracket
763763 -> Unfold m (c, e) b -- ^ on exception
764764 -> Unfold m c b -- ^ unfold to run
765765 -> Unfold m a b
766−gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
766+gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
767767 Unfold step inject
768768
769769 where
@@ -797,8 +797,8 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
797797--
798798-- /Internal/
799799--
800−{-# INLINE_NORMAL gbracketIO #-}
801−gbracketIO
800+{-# INLINE_NORMAL gbracket #-}
801+gbracket
802802 :: (MonadIO m, MonadBaseControl IO m)
803803 => (a -> m c) -- ^ before
804804 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -806,7 +806,7 @@ gbracketIO
806806 -> Unfold m (c, e) b -- ^ on exception
807807 -> Unfold m c b -- ^ unfold to run
808808 -> Unfold m a b
809−gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
809+gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
810810 Unfold step inject
811811
812812 where
@@ -848,7 +848,7 @@ gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
848848--
849849{-# INLINE_NORMAL _before #-}
850850_before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
851−_before action = gbracket (\x -> action x >> return x) (fmap Right)
851+_before action = gbracket_ (\x -> action x >> return x) (fmap Right)
852852 (\_ -> return ()) undefined
853853
854854-- | Run a side effect before the unfold yields its first element.
@@ -874,18 +874,18 @@ before action (Unfold step1 inject1) = Unfold step inject
874874
875875{-# INLINE_NORMAL _after #-}
876876_after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
877−_after aft = gbracket return (fmap Right) aft undefined
877+_after aft = gbracket_ return (fmap Right) aft undefined
878878
879879-- | Run a side effect whenever the unfold stops normally.
880880--
881−-- Prefer afterIO over this as the @after@ action in this combinator is not
881+-- Prefer 'after' over this as the @after@ action in this combinator is not
882882-- executed if the unfold is partially evaluated lazily and then garbage
883883-- collected.
884884--
885885-- /Internal/
886−{-# INLINE_NORMAL after #-}
887−after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
888−after action (Unfold step1 inject1) = Unfold step inject
886+{-# INLINE_NORMAL after_ #-}
887+after_ :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
888+after_ action (Unfold step1 inject1) = Unfold step inject
889889
890890 where
891891
@@ -905,10 +905,10 @@ after action (Unfold step1 inject1) = Unfold step inject
905905-- or is garbage collected after a partial lazy evaluation.
906906--
907907-- /Internal/
908−{-# INLINE_NORMAL afterIO #-}
909−afterIO :: (MonadIO m, MonadBaseControl IO m)
908+{-# INLINE_NORMAL after #-}
909+after :: (MonadIO m, MonadBaseControl IO m)
910910 => (a -> m c) -> Unfold m a b -> Unfold m a b
911−afterIO action (Unfold step1 inject1) = Unfold step inject
911+after action (Unfold step1 inject1) = Unfold step inject
912912
913913 where
914914
@@ -930,7 +930,7 @@ afterIO action (Unfold step1 inject1) = Unfold step inject
930930{-# INLINE_NORMAL _onException #-}
931931_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
932932_onException action =
933− gbracket return MC.try
933+ gbracket_ return MC.try
934934 (\_ -> return ())
935935 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
936936
@@ -958,20 +958,20 @@ onException action (Unfold step1 inject1) = Unfold step inject
958958{-# INLINE_NORMAL _finally #-}
959959_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
960960_finally action =
961− gbracket return MC.try action
961+ gbracket_ return MC.try action
962962 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
963963
964964-- | Run a side effect whenever the unfold stops normally or aborts due to an
965965-- exception.
966966--
967−-- Prefer finallyIO over this as the @after@ action in this combinator is not
967+-- Prefer 'finally' over this as the @after@ action in this combinator is not
968968-- executed if the unfold is partially evaluated lazily and then garbage
969969-- collected.
970970--
971971-- /Internal/
972−{-# INLINE_NORMAL finally #-}
973−finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
974−finally action (Unfold step1 inject1) = Unfold step inject
972+{-# INLINE_NORMAL finally_ #-}
973+finally_ :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
974+finally_ action (Unfold step1 inject1) = Unfold step inject
975975
976976 where
977977
@@ -991,10 +991,10 @@ finally action (Unfold step1 inject1) = Unfold step inject
991991-- exception or if it is garbage collected after a partial lazy evaluation.
992992--
993993-- /Internal/
994−{-# INLINE_NORMAL finallyIO #-}
995−finallyIO :: (MonadAsync m, MonadCatch m)
994+{-# INLINE_NORMAL finally #-}
995+finally :: (MonadAsync m, MonadCatch m)
996996 => (a -> m c) -> Unfold m a b -> Unfold m a b
997−finallyIO action (Unfold step1 inject1) = Unfold step inject
997+finally action (Unfold step1 inject1) = Unfold step inject
998998
999999 where
10001000
@@ -1017,23 +1017,23 @@ finallyIO action (Unfold step1 inject1) = Unfold step inject
10171017_bracket :: MonadCatch m
10181018 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
10191019_bracket bef aft =
1020− gbracket bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
1020+ gbracket_ bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
10211021 MC.throwM e))
10221022
1023−-- | @bracket before after between@ runs the @before@ action and then unfolds
1023+-- | @bracket_ before after between@ runs the @before@ action and then unfolds
10241024-- its output using the @between@ unfold. When the @between@ unfold is done or
10251025-- if an exception occurs then the @after@ action is run with the output of
10261026-- @before@ as argument.
10271027--
1028−-- Prefer bracketIO over this as the @after@ action in this combinator is not
1028+-- Prefer 'bracket' over this as the @after@ action in this combinator is not
10291029-- executed if the unfold is partially evaluated lazily and then garbage
10301030-- collected.
10311031--
10321032-- /Internal/
1033−{-# INLINE_NORMAL bracket #-}
1034−bracket :: MonadCatch m
1033+{-# INLINE_NORMAL bracket_ #-}
1034+bracket_ :: MonadCatch m
10351035 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1036−bracket bef aft (Unfold step1 inject1) = Unfold step inject
1036+bracket_ bef aft (Unfold step1 inject1) = Unfold step inject
10371037
10381038 where
10391039
@@ -1057,10 +1057,10 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
10571057-- paritally evaluated and then garbage collected.
10581058--
10591059-- /Internal/
1060−{-# INLINE_NORMAL bracketIO #-}
1061−bracketIO :: (MonadAsync m, MonadCatch m)
1060+{-# INLINE_NORMAL bracket #-}
1061+bracket :: (MonadAsync m, MonadCatch m)
10621062 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1063−bracketIO bef aft (Unfold step1 inject1) = Unfold step inject
1063+bracket bef aft (Unfold step1 inject1) = Unfold step inject
10641064
10651065 where
10661066
@@ -1092,4 +1092,4 @@ bracketIO bef aft (Unfold step1 inject1) = Unfold step inject
10921092handle :: (MonadCatch m, Exception e)
10931093 => Unfold m e b -> Unfold m a b -> Unfold m a b
10941094handle exc =
1095− gbracket return MC.try (\_ -> return ()) (discardFirst exc)
1095+ gbracket_ return MC.try (\_ -> return ()) (discardFirst exc)
src/Streamly/Internal/FileSystem/File.hs+10−10
@@ -138,7 +138,7 @@ import qualified Streamly.Prelude as S
138138-- /Internal/
139139--
140140{-# INLINE withFile #-}
141−withFile :: (IsStream t, MonadCatch m, MonadIO m)
141+withFile :: (IsStream t, MonadCatch m, MonadAsync m)
142142 => FilePath -> IOMode -> (Handle -> t m a) -> t m a
143143withFile file mode = S.bracket (liftIO $ openFile file mode) (liftIO . hClose)
144144
@@ -151,14 +151,14 @@ withFile file mode = S.bracket (liftIO $ openFile file mode) (liftIO . hClose)
151151-- /Internal/
152152--
153153{-# INLINABLE usingFile #-}
154−usingFile :: (MonadCatch m, MonadIO m)
154+usingFile :: (MonadCatch m, MonadAsync m)
155155 => Unfold m Handle a -> Unfold m FilePath a
156156usingFile =
157157 UF.bracket (\file -> liftIO $ openFile file ReadMode)
158158 (liftIO . hClose)
159159
160160{-# INLINABLE usingFile2 #-}
161−usingFile2 :: (MonadCatch m, MonadIO m)
161+usingFile2 :: (MonadCatch m, MonadAsync m)
162162 => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
163163usingFile2 = UF.bracket before after
164164
@@ -202,7 +202,7 @@ appendArray file arr = SIO.withFile file AppendMode (`FH.writeArray` arr)
202202-- The maximum size of a single array is specified by @size@. The actual size
203203-- read may be less than or equal to @size@.
204204{-# INLINABLE toChunksWithBufferOf #-}
205−toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadIO m)
205+toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadAsync m)
206206 => Int -> FilePath -> t m (Array Word8)
207207toChunksWithBufferOf size file =
208208 withFile file ReadMode (FH.toChunksWithBufferOf size)
@@ -217,7 +217,7 @@ toChunksWithBufferOf size file =
217217--
218218-- @since 0.7.0
219219{-# INLINE toChunks #-}
220−toChunks :: (IsStream t, MonadCatch m, MonadIO m)
220+toChunks :: (IsStream t, MonadCatch m, MonadAsync m)
221221 => FilePath -> t m (Array Word8)
222222toChunks = toChunksWithBufferOf defaultChunkSize
223223
@@ -237,7 +237,7 @@ toChunks = toChunksWithBufferOf defaultChunkSize
237237-- /Internal/
238238--
239239{-# INLINE readChunksWithBufferOf #-}
240−readChunksWithBufferOf :: (MonadCatch m, MonadIO m)
240+readChunksWithBufferOf :: (MonadCatch m, MonadAsync m)
241241 => Unfold m (Int, FilePath) (Array Word8)
242242readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
243243
@@ -249,7 +249,7 @@ readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
249249--
250250-- /Internal/
251251{-# INLINE readChunks #-}
252−readChunks :: (MonadCatch m, MonadIO m) => Unfold m FilePath (Array Word8)
252+readChunks :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
253253readChunks = usingFile FH.readChunks
254254
255255-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
@@ -257,7 +257,7 @@ readChunks = usingFile FH.readChunks
257257--
258258-- /Internal/
259259{-# INLINE readWithBufferOf #-}
260−readWithBufferOf :: (MonadCatch m, MonadIO m) => Unfold m (Int, FilePath) Word8
260+readWithBufferOf :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
261261readWithBufferOf = usingFile2 FH.readWithBufferOf
262262
263263-- | Unfolds a file path into a byte stream. IO requests to the device are
@@ -266,7 +266,7 @@ readWithBufferOf = usingFile2 FH.readWithBufferOf
266266--
267267-- @since 0.7.0
268268{-# INLINE read #-}
269−read :: (MonadCatch m, MonadIO m) => Unfold m FilePath Word8
269+read :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
270270read = UF.concat (usingFile FH.readChunks) A.read
271271
272272-- | Generate a stream of bytes from a file specified by path. The stream ends
@@ -276,7 +276,7 @@ read = UF.concat (usingFile FH.readChunks) A.read
276276-- /Internal/
277277--
278278{-# INLINE toBytes #-}
279−toBytes :: (IsStream t, MonadCatch m, MonadIO m) => FilePath -> t m Word8
279+toBytes :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m Word8
280280toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
281281
282282{-
src/Streamly/Internal/Network/Inet/TCP.hs+4−4
@@ -278,7 +278,7 @@ withConnectionM addr port =
278278--
279279-- /Internal/
280280{-# INLINABLE usingConnection #-}
281−usingConnection :: (MonadCatch m, MonadIO m)
281+usingConnection :: (MonadCatch m, MonadAsync m)
282282 => Unfold m Socket a
283283 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
284284usingConnection =
@@ -298,7 +298,7 @@ usingConnection =
298298--
299299-- /Internal/
300300{-# INLINABLE withConnection #-}
301−withConnection :: (IsStream t, MonadCatch m, MonadIO m)
301+withConnection :: (IsStream t, MonadCatch m, MonadAsync m)
302302 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
303303withConnection addr port =
304304 S.bracket (liftIO $ connect addr port) (liftIO . Net.close)
@@ -311,7 +311,7 @@ withConnection addr port =
311311--
312312-- @since 0.7.0
313313{-# INLINE read #-}
314−read :: (MonadCatch m, MonadIO m)
314+read :: (MonadCatch m, MonadAsync m)
315315 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
316316read = UF.concat (usingConnection ISK.readChunks) A.read
317317
@@ -319,7 +319,7 @@ read = UF.concat (usingConnection ISK.readChunks) A.read
319319--
320320-- @since 0.7.0
321321{-# INLINE toBytes #-}
322−toBytes :: (IsStream t, MonadCatch m, MonadIO m)
322+toBytes :: (IsStream t, MonadCatch m, MonadAsync m)
323323 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
324324toBytes addr port = AS.concat $ withConnection addr port ISK.toChunks
325325
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -122,7 +122,7 @@ handleWithM f sk = finally (f sk) (liftIO (Net.close sk))
122122--
123123-- @since 0.7.0
124124{-# INLINE handleWith #-}
125−handleWith :: (IsStream t, MonadCatch m, MonadIO m)
125+handleWith :: (IsStream t, MonadAsync m, MonadCatch m)
126126 => Socket -> (Socket -> t m a) -> t m a
127127handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
128128
src/Streamly/Internal/Prelude.hs+27−27
@@ -481,13 +481,13 @@ module Streamly.Internal.Prelude
481481
482482 -- * Exceptions
483483 , before
484+ , after_
484485 , after
485− , afterIO
486+ , bracket_
486487 , bracket
487− , bracketIO
488488 , onException
489+ , finally_
489490 , finally
490− , finallyIO
491491 , handle
492492
493493 -- * Generalize Inner Monad
@@ -4831,23 +4831,23 @@ before action xs = D.fromStreamD $ D.before action $ D.toStreamD xs
48314831
48324832-- | Run a side effect whenever the stream stops normally.
48334833--
4834−-- Prefer 'afterIO' over this as the @after@ action in this combinator is not
4835−-- executed if the unfold is partially evaluated lazily and then garbage
4834+-- Prefer 'after' over this as the @after@ action in this combinator is not
4835+-- executed if the stream is partially evaluated lazily and then garbage
48364836-- collected.
48374837--
48384838-- @since 0.7.0
4839−{-# INLINE after #-}
4840−after :: (IsStream t, Monad m) => m b -> t m a -> t m a
4841−after action xs = D.fromStreamD $ D.after action $ D.toStreamD xs
4839+{-# INLINE after_ #-}
4840+after_ :: (IsStream t, Monad m) => m b -> t m a -> t m a
4841+after_ action xs = D.fromStreamD $ D.after_ action $ D.toStreamD xs
48424842
48434843-- | Run a side effect whenever the stream stops normally
48444844-- or is garbage collected after a partial lazy evaluation.
48454845--
48464846-- /Internal/
48474847--
4848−{-# INLINE afterIO #-}
4849−afterIO :: (IsStream t, MonadIO m, MonadBaseControl IO m) => m b -> t m a -> t m a
4850−afterIO action xs = D.fromStreamD $ D.afterIO action $ D.toStreamD xs
4848+{-# INLINE after #-}
4849+after :: (IsStream t, MonadIO m, MonadBaseControl IO m) => m b -> t m a -> t m a
4850+after action xs = D.fromStreamD $ D.after action $ D.toStreamD xs
48514851
48524852-- | Run a side effect whenever the stream aborts due to an exception.
48534853--
@@ -4859,39 +4859,39 @@ onException action xs = D.fromStreamD $ D.onException action $ D.toStreamD xs
48594859-- | Run a side effect whenever the stream stops normally or aborts due to an
48604860-- exception.
48614861--
4862−-- Prefer 'finallyIO' over this as the @after@ action in this combinator is not
4862+-- Prefer 'finally' over this as the @after@ action in this combinator is not
48634863-- executed if the unfold is partially evaluated lazily and then garbage
48644864-- collected.
48654865--
48664866-- @since 0.7.0
4867−{-# INLINE finally #-}
4868−finally :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
4869−finally action xs = D.fromStreamD $ D.finally action $ D.toStreamD xs
4867+{-# INLINE finally_ #-}
4868+finally_ :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
4869+finally_ action xs = D.fromStreamD $ D.finally_ action $ D.toStreamD xs
48704870
48714871-- | Run a side effect whenever the stream stops normally, aborts due to an
48724872-- exception or if it is garbage collected after a partial lazy evaluation.
48734873--
48744874-- /Internal/
48754875--
4876−{-# INLINE finallyIO #-}
4877−finallyIO :: (IsStream t, MonadAsync m, MonadCatch m) => m b -> t m a -> t m a
4878−finallyIO action xs = D.fromStreamD $ D.finallyIO action $ D.toStreamD xs
4876+{-# INLINE finally #-}
4877+finally :: (IsStream t, MonadAsync m, MonadCatch m) => m b -> t m a -> t m a
4878+finally action xs = D.fromStreamD $ D.finally action $ D.toStreamD xs
48794879
48804880-- | Run the first action before the stream starts and remember its output,
48814881-- generate a stream using the output, run the second action using the
48824882-- remembered value as an argument whenever the stream ends normally or due to
48834883-- an exception.
48844884--
4885−-- Prefer 'bracketIO' over this as the @after@ action in this combinator is not
4885+-- Prefer 'bracket' over this as the @after@ action in this combinator is not
48864886-- executed if the unfold is partially evaluated lazily and then garbage
48874887-- collected.
48884888--
48894889-- @since 0.7.0
4890−{-# INLINE bracket #-}
4891−bracket :: (IsStream t, MonadCatch m)
4890+{-# INLINE bracket_ #-}
4891+bracket_ :: (IsStream t, MonadCatch m)
48924892 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4893−bracket bef aft bet = D.fromStreamD $
4894− D.bracket bef aft (toStreamD . bet)
4893+bracket_ bef aft bet = D.fromStreamD $
4894+ D.bracket_ bef aft (toStreamD . bet)
48954895
48964896-- | Run the first action before the stream starts and remember its output,
48974897-- generate a stream using the output, run the second action using the
@@ -4900,11 +4900,11 @@ bracket bef aft bet = D.fromStreamD $
49004900--
49014901-- /Internal/
49024902--
4903−{-# INLINE bracketIO #-}
4904−bracketIO :: (IsStream t, MonadAsync m, MonadCatch m)
4903+{-# INLINE bracket #-}
4904+bracket :: (IsStream t, MonadAsync m, MonadCatch m)
49054905 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4906−bracketIO bef aft bet = D.fromStreamD $
4907− D.bracketIO bef aft (toStreamD . bet)
4906+bracket bef aft bet = D.fromStreamD $
4907+ D.bracket bef aft (toStreamD . bet)
49084908
49094909-- | When evaluating a stream if an exception occurs, stream evaluation aborts
49104910-- and the specified exception handler is run with the exception as argument.