Skip to content

Commit 45ca67b

Browse files
committed
Modify asyncRegister_ to link to the main thread before cancelling in
the cleanUp action
1 parent 443edc6 commit 45ca67b

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/Hedgehog/Extras/Test/Concurrent.hs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,47 +60,53 @@ Please note that only @FAIL1@ and @FAIL2@ annotations were reported - @FAIL3@ an
6060
below was swallowed without any information.
6161
6262
__Don't use concurrency abstractions from this module, when you need to aggregate and report failures!__
63-
6463
-}
65-
module Hedgehog.Extras.Test.Concurrent
66-
( threadDelay
67-
, asyncRegister_
68-
-- * Re-exports of concurrency abstractions from @lifted-base@
69-
, module Control.Concurrent.Async.Lifted
70-
, module Control.Concurrent.MVar.Lifted
71-
, module System.Timeout.Lifted
72-
) where
64+
module Hedgehog.Extras.Test.Concurrent (
65+
threadDelay,
66+
asyncRegister_,
67+
68+
-- * Re-exports of concurrency abstractions from @lifted-base@
69+
module Control.Concurrent.Async.Lifted,
70+
module Control.Concurrent.MVar.Lifted,
71+
module System.Timeout.Lifted,
72+
) where
7373

74-
import Control.Concurrent.Async.Lifted
74+
import Control.Concurrent.Async.Lifted
7575
import qualified Control.Concurrent.Lifted as IO
76-
import Control.Concurrent.MVar.Lifted
77-
import Control.Monad.IO.Class
78-
import Control.Monad.Trans.Resource
79-
import Data.Function
80-
import Data.Int
76+
import Control.Concurrent.MVar.Lifted
77+
import Control.Monad.IO.Class
78+
import Control.Monad.Trans.Resource
79+
import Data.Function
80+
import Data.Int
8181
import qualified GHC.Stack as GHC
82-
import System.IO (IO)
83-
import System.Timeout.Lifted
84-
import Hedgehog.Extras.Internal.Orphans ()
82+
import Hedgehog.Extras.Internal.Orphans ()
83+
import System.IO (IO)
84+
import System.Timeout.Lifted
8585

86-
import Control.Monad
87-
import Control.Monad.Catch (MonadCatch)
88-
import GHC.Stack
89-
import Hedgehog
86+
import Control.Monad
87+
import GHC.Stack
88+
import Hedgehog
9089
import qualified Hedgehog as H
9190

9291
-- | Delay the thread by 'n' microseconds.
9392
threadDelay :: (HasCallStack, MonadTest m, MonadIO m) => Int -> m ()
9493
threadDelay n = GHC.withFrozenCallStack . H.evalIO $ IO.threadDelay n
9594

9695
-- | Runs an action in background, and registers its cancellation to 'MonadResource'.
97-
asyncRegister_ :: HasCallStack
98-
=> MonadTest m
99-
=> MonadResource m
100-
=> MonadCatch m
101-
=> IO a -- ^ Action to run in background
102-
-> m ()
103-
asyncRegister_ act = GHC.withFrozenCallStack $ void . H.evalM $ allocate (async act) cleanUp
96+
asyncRegister_ ::
97+
(HasCallStack) =>
98+
(MonadResource m) =>
99+
-- | Action to run in background
100+
IO a ->
101+
m (ReleaseKey, Async a)
102+
asyncRegister_ act = GHC.withFrozenCallStack $ do
103+
allocate
104+
( do
105+
a <- async act
106+
link a
107+
return a
108+
)
109+
cleanUp
104110
where
105111
cleanUp :: Async a -> IO ()
106-
cleanUp a = cancel a >> void (link a)
112+
cleanUp = cancel

0 commit comments

Comments
 (0)