From 6ea0264d32d5337c033d3865e16f6f35dfbe47f0 Mon Sep 17 00:00:00 2001 From: Lia Lenckowski Date: Mon, 28 Aug 2023 12:08:53 +0200 Subject: don't deadlock on multiple stops --- src/BatchedRunner.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/BatchedRunner.hs b/src/BatchedRunner.hs index 83a704c..89dd3a8 100644 --- a/src/BatchedRunner.hs +++ b/src/BatchedRunner.hs @@ -31,7 +31,14 @@ makeRunner f n = do unless (fromMaybe False mayB) $ runnerLoop mv stopRunner :: Runner -> IO () -stopRunner (Runner mv) = atomically $ putTMVar mv True +stopRunner (Runner mv) = atomically $ do + mayB <- tryReadTMVar mv + case mayB of + -- if stopped/stopping, do nothing + Just True -> return () + -- if idle or working, block, such that the Runner has a chance to see that + -- it should fire before stopping + _ -> putTMVar mv True notifyRunner :: Runner -> IO () notifyRunner (Runner mv) = void $ atomically $ tryPutTMVar mv False -- cgit v1.2.3-70-g09d2