2-- Module : Streamly.Internal.Data.Stream.StreamD.Exception
3-- Copyright : (c) 2020 Composewell Technologies and Contributors
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.StreamD.Exception
28import Control.Exception (Exception, SomeException, mask_)
29import Control.Monad.Catch (MonadCatch)
30import Control.Monad.IO.Class (MonadIO(..))
31import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
32import GHC.Exts (inline)
33import Streamly.Internal.Data.IOFinalizer
34 (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
36import qualified Control.Monad.Catch as MC
38import Streamly.Internal.Data.Stream.StreamD.Type
39import Streamly.Internal.Data.SVar
41data GbracketState s1 s2 v
44 | GBracketException s2
46-- | Like 'gbracket' but with following differences:
48-- * alloc action @m c@ runs with async exceptions enabled
49-- * cleanup action @c -> m d@ won't run if the stream is garbage collected
50-- after partial evaluation.
51-- * does not require a 'MonadAsync' constraint.
53-- /Inhibits stream fusion/
57{-# INLINE_NORMAL gbracket_ #-}
61 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
62 -> (c -> m d) -- ^ after, on normal stop
63 -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
64 -> (c -> Stream m b) -- ^ stream generator
66gbracket_ bef exc aft fexc fnormal =
67 Stream step GBracketInit
71 {-# INLINE_LATE step #-}
72 step _ GBracketInit = do
74 return $ Skip $ GBracketNormal (fnormal r) r
76 step gst (GBracketNormal (UnStream step1 st) v) = do
77 res <- exc $ step1 gst st
81 return $ Yield x (GBracketNormal (Stream step1 s) v)
82 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
83 Stop -> aft v >> return Stop
84 -- XXX Do not handle async exceptions, just rethrow them.
86 return $ Skip (GBracketException (fexc v e (UnStream step1 st)))
87 step gst (GBracketException (UnStream step1 st)) = do
90 Yield x s -> return $ Yield x (GBracketException (Stream step1 s))
91 Skip s -> return $ Skip (GBracketException (Stream step1 s))
94data GbracketIOState s1 s2 v wref
96 | GBracketIONormal s1 v wref
97 | GBracketIOException s2
99-- | Run the alloc action @m c@ with async exceptions disabled but keeping
100-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
101-- output @c@ as input to @c -> Stream m b@ to generate an output stream. When
102-- generating the stream use the supplied @try@ operation @forall s. m s -> m
103-- (Either e s)@ to catch synchronous exceptions. If an exception occurs run
104-- the exception handler @c -> e -> Stream m b -> m (Stream m b)@.
106-- The cleanup action @c -> m d@, runs whenever the stream ends normally, due
107-- to a sync or async exception or if it gets garbage collected after a partial
108-- lazy evaluation. See 'bracket' for the semantics of the cleanup action.
110-- 'gbracket' can express all other exception handling combinators.
112-- /Inhibits stream fusion/
115{-# INLINE_NORMAL gbracket #-}
117 :: (MonadIO m, MonadBaseControl IO m)
119 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
120 -> (c -> m d) -- ^ after, on normal stop or GC
121 -> (c -> e -> Stream m b -> m (Stream m b)) -- ^ on exception
122 -> (c -> Stream m b) -- ^ stream generator
124gbracket bef exc aft fexc fnormal =
125 Stream step GBracketIOInit
129 -- If the stream is never evaluated the "aft" action will never be
130 -- called. For that to occur we will need the user of this API to pass a
131 -- weak pointer to us.
132 {-# INLINE_LATE step #-}
133 step _ GBracketIOInit = do
134 -- We mask asynchronous exceptions to make the execution
135 -- of 'bef' and the registration of 'aft' atomic.
136 -- A similar thing is done in the resourcet package: https://git.io/JvKV3
137 -- Tutorial: https://markkarpov.com/tutorial/exceptions.html
138 (r, ref) <- liftBaseOp_ mask_ $ do
140 ref <- newIOFinalizer (aft r)
142 return $ Skip $ GBracketIONormal (fnormal r) r ref
144 step gst (GBracketIONormal (UnStream step1 st) v ref) = do
145 res <- exc $ step1 gst st
149 return $ Yield x (GBracketIONormal (Stream step1 s) v ref)
151 return $ Skip (GBracketIONormal (Stream step1 s) v ref)
155 -- XXX Do not handle async exceptions, just rethrow them.
157 -- Clearing of finalizer and running of exception handler must
158 -- be atomic wrt async exceptions. Otherwise if we have cleared
159 -- the finalizer and have not run the exception handler then we
160 -- may leak the resource.
161 stream <- clearingIOFinalizer ref (fexc v e (UnStream step1 st))
162 return $ Skip (GBracketIOException stream)
163 step gst (GBracketIOException (UnStream step1 st)) = do
167 return $ Yield x (GBracketIOException (Stream step1 s))
168 Skip s -> return $ Skip (GBracketIOException (Stream step1 s))
171-- | See 'Streamly.Internal.Data.Stream.IsStream.before'.
173{-# INLINE_NORMAL before #-}
174before :: Monad m => m b -> Stream m a -> Stream m a
175before action (Stream step state) = Stream step' Nothing
179 {-# INLINE_LATE step' #-}
180 step' _ Nothing = action >> return (Skip (Just state))
182 step' gst (Just st) = do
185 Yield x s -> return $ Yield x (Just s)
186 Skip s -> return $ Skip (Just s)
189-- | See 'Streamly.Internal.Data.Stream.IsStream.after_'.
191{-# INLINE_NORMAL after_ #-}
192after_ :: Monad m => m b -> Stream m a -> Stream m a
193after_ action (Stream step state) = Stream step' state
197 {-# INLINE_LATE step' #-}
201 Yield x s -> return $ Yield x s
202 Skip s -> return $ Skip s
203 Stop -> action >> return Stop
205-- | See 'Streamly.Internal.Data.Stream.IsStream.after'.
207{-# INLINE_NORMAL after #-}
208after :: (MonadIO m, MonadBaseControl IO m)
209 => m b -> Stream m a -> Stream m a
210after action (Stream step state) = Stream step' Nothing
214 {-# INLINE_LATE step' #-}
216 ref <- newIOFinalizer action
217 return $ Skip $ Just (state, ref)
218 step' gst (Just (st, ref)) = do
221 Yield x s -> return $ Yield x (Just (s, ref))
222 Skip s -> return $ Skip (Just (s, ref))
227-- XXX For high performance error checks in busy streams we may need another
228-- Error constructor in step.
230-- | See 'Streamly.Internal.Data.Stream.IsStream.onException'.
232{-# INLINE_NORMAL onException #-}
233onException :: MonadCatch m => m b -> Stream m a -> Stream m a
234onException action str =
235 gbracket_ (return ()) (inline MC.try) return
236 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
239{-# INLINE_NORMAL _onException #-}
240_onException :: MonadCatch m => m b -> Stream m a -> Stream m a
241_onException action (Stream step state) = Stream step' state
245 {-# INLINE_LATE step' #-}
247 res <- step gst st `MC.onException` action
249 Yield x s -> return $ Yield x s
250 Skip s -> return $ Skip s
253-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket_'.
255{-# INLINE_NORMAL bracket_ #-}
256bracket_ :: MonadCatch m
257 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
258bracket_ bef aft bet =
259 gbracket_ bef (inline MC.try) aft
260 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e)) bet
262-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
264{-# INLINE_NORMAL bracket #-}
265bracket :: (MonadAsync m, MonadCatch m)
266 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
268 gbracket bef (inline MC.try) aft
269 (\a (e :: SomeException) _ -> aft a >> return (nilM (MC.throwM e))) bet
271data BracketState s v = BracketInit | BracketRun s v
273-- | Alternate (custom) implementation of 'bracket'.
275{-# INLINE_NORMAL _bracket #-}
276_bracket :: MonadCatch m
277 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
278_bracket bef aft bet = Stream step' BracketInit
282 {-# INLINE_LATE step' #-}
283 step' _ BracketInit = bef >>= \x -> return (Skip (BracketRun (bet x) x))
285 -- NOTE: It is important to use UnStream instead of the Stream pattern
286 -- here, otherwise we get huge perf degradation, see note in concatMap.
287 step' gst (BracketRun (UnStream step state) v) = do
288 -- res <- step gst state `MC.onException` aft v
289 res <- inline MC.try $ step gst state
291 Left (e :: SomeException) -> aft v >> MC.throwM e >> return Stop
293 Yield x s -> return $ Yield x (BracketRun (Stream step s) v)
294 Skip s -> return $ Skip (BracketRun (Stream step s) v)
295 Stop -> aft v >> return Stop
297-- | See 'Streamly.Internal.Data.Stream.IsStream.finally_'.
299{-# INLINE finally_ #-}
300finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
301finally_ action xs = bracket_ (return ()) (\_ -> action) (const xs)
303-- | See 'Streamly.Internal.Data.Stream.IsStream.finally'.
305-- finally action xs = after action $ onException action xs
307{-# INLINE finally #-}
308finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
309finally action xs = bracket (return ()) (\_ -> action) (const xs)
311-- | See 'Streamly.Internal.Data.Stream.IsStream.ghandle'.
313{-# INLINE_NORMAL ghandle #-}
314ghandle :: (MonadCatch m, Exception e)
315 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
317 gbracket_ (return ()) (inline MC.try) return (\_ -> f) (\_ -> str)
319-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
321{-# INLINE_NORMAL handle #-}
322handle :: (MonadCatch m, Exception e)
323 => (e -> Stream m a) -> Stream m a -> Stream m a
325 gbracket_ (return ()) (inline MC.try) return (\_ e _ -> f e) (\_ -> str)
327-- | Alternate (custom) implementation of 'handle'.
329{-# INLINE_NORMAL _handle #-}
330_handle :: (MonadCatch m, Exception e)
331 => (e -> Stream m a) -> Stream m a -> Stream m a
332_handle f (Stream step state) = Stream step' (Left state)
336 {-# INLINE_LATE step' #-}
337 step' gst (Left st) = do
338 res <- inline MC.try $ step gst st
340 Left e -> return $ Skip $ Right (f e)
342 Yield x s -> return $ Yield x (Left s)
343 Skip s -> return $ Skip (Left s)
346 step' gst (Right (UnStream step1 st)) = do
349 Yield x s -> return $ Yield x (Right (Stream step1 s))
350 Skip s -> return $ Skip (Right (Stream step1 s))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list