From 9b27d16047eeda4f8f64a4016b14f0349e64925e Mon Sep 17 00:00:00 2001 From: Guillaume Bouchard Date: Sun, 22 Sep 2024 12:03:17 +0400 Subject: [PATCH] fix: mitigate sydtest crash when used recursively This closes https://github.com/NorfairKing/sydtest/issues/91 by avoiding a call to `withArgs` if the argument list is already empty. `withArgs` can crash in a concurrent environment (see GHC BUG https://gitlab.haskell.org/ghc/ghc/-/issues/18261), hence we reduce the chance of this happening by not calling it when the list of arguments is already in the required state (e.g. empty). --- sydtest/src/Test/Syd/Runner.hs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sydtest/src/Test/Syd/Runner.hs b/sydtest/src/Test/Syd/Runner.hs index 3073a99..7d75110 100644 --- a/sydtest/src/Test/Syd/Runner.hs +++ b/sydtest/src/Test/Syd/Runner.hs @@ -36,7 +36,22 @@ import Text.Printf -- it removes all the arguments initially provided to sydtest and provides a -- reproducible environment. setNullArgs :: IO a -> IO a -setNullArgs action = withArgs [] action +setNullArgs action = do + -- Check that args are not empty before setting it to empty. + -- This is a workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/18261 + -- In summary, `withArgs` is not thread-safe, hence we would like to avoid it + -- as much as possible. + -- + -- If sydtest is used in a more complex environment which may use `withArgs` + -- too, we would like to avoid a complete crash of the program. + -- + -- Especially, if sydtest is used itself in a sydtest test (e.g. in order to + -- test sydtest command line itself), it may crash, see + -- https://github.com/NorfairKing/sydtest/issues/91 for details. + args <- getArgs + if null args + then action + else withArgs [] action sydTestResult :: Settings -> TestDefM '[] () r -> IO (Timed ResultForest) sydTestResult settings spec = do