runProcessM :: TestConfig -> ProcessM a -> IO (a,[[Response]])
runProcessM c m = do
    ps <- mapM (launchWorker c) [0..nProcesses c-1]
    (fmap (\(a,(_,rs))-> (a,map reverse rs))$ runStateT m (ps,map (const []) ps))
      `finally` (do forM_ ps (\p -> terminateProcess (ph p))
                    forM_ ps (\p -> waitForProcess (ph p))
                    when (withValgrind c)$
                      forM_ [0..length ps-1] checkValgrindMessages
                )
  where
    checkValgrindMessages pid =
      bracket
        (openBinaryFile (workerStderr pid) ReadMode)
        hClose
        (\h -> do
          s <- hGetContents h
          when ("==    at " `isInfixOf` s)$ ioError$ userError$ "valgrind found errors"
        )
runProcessM :: TestConfig -> ProcessM a -> IO (a,[[Response]])
runProcessM c m = do
    ps <- mapM (launchWorker c) [0..nProcesses c-1]
    (fmap (\(a,(_,rs))-> (a,map reverse rs))$ runStateT m (ps,map (const []) ps))
      `finally` (do forM_ ps (\p -> terminateProcess (ph p))
                    forM_ ps (\p -> waitForProcess (ph p))
                    when (withValgrind c)$
                      forM_ [0..length ps-1] checkValgrindMessages
                )
  where
    checkValgrindMessages pid =
      bracket
        (openBinaryFile (workerStderr pid) ReadMode)
        hClose
        (\h -> do
          s <- hGetContents h
          when ("==    at " `isInfixOf` s)$ ioError$ userError$ "valgrind found errors"
        )
runProcessM c m = do
    ps <- mapM (launchWorker c) [0..nProcesses c-1]
    (fmap (\(a,(_,rs))-> (a,map reverse rs))$ runStateT m (ps,map (const []) ps))
      `finally` (do forM_ ps (\p -> terminateProcess (ph p))
                    forM_ ps (\p -> waitForProcess (ph p))
                    when (withValgrind c)$
                      forM_ [0..length ps-1] checkValgrindMessages
                )
  where
    checkValgrindMessages pid =
      bracket
        (openBinaryFile (workerStderr pid) ReadMode)
        hClose
        (\h -> do
          s <- hGetContents h
          when ("==    at " `isInfixOf` s)$ ioError$ userError$ "valgrind found errors"
        )