Skip to content

Instantly share code, notes, and snippets.

@Gabriella439
Created August 18, 2022 00:39
Show Gist options
  • Save Gabriella439/193de4cd489c4a81c991b07d85a79d3b to your computer and use it in GitHub Desktop.
Save Gabriella439/193de4cd489c4a81c991b07d85a79d3b to your computer and use it in GitHub Desktop.
Example async-exception-safe retry function
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Concurrent (threadDelay)
import Control.Exception
retry :: Int -> IO a -> IO a
retry n io
| n <= 0 = io
| otherwise = io `catch` \(_ :: SomeException) -> do
mask \restore -> do
restore (threadDelay 1000000)
`onException` restore (retry (n - 1) io)
restore (retry (n - 1) io)
main :: IO ()
main = do
retry 4 (print 1)
retry 4 (fail "Urk!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment