[Git][ghc/ghc][wip/T18069] SysTools.Process: Handle exceptions in readCreateProcessWithExitCode'

Ben Gamari gitlab at gitlab.haskell.org
Wed May 20 16:25:40 UTC 2020



Ben Gamari pushed to branch wip/T18069 at Glasgow Haskell Compiler / GHC


Commits:
1b7719bf by Ben Gamari at 2020-05-20T12:25:31-04:00
SysTools.Process: Handle exceptions in readCreateProcessWithExitCode'

In #18069 we are observing MVar deadlocks from somewhere in ghc.exe.
This use of MVar stood out as being one of the more likely culprits.
Here we make sure that it is exception-safe.

- - - - -


1 changed file:

- compiler/GHC/SysTools/Process.hs


Changes:

=====================================
compiler/GHC/SysTools/Process.hs
=====================================
@@ -41,6 +41,15 @@ enableProcessJobs opts = opts { use_process_jobs = True }
 enableProcessJobs opts = opts
 #endif
 
+#if !MIN_VERSION_base(4,15,0)
+-- TODO: This can be dropped with GHC 8.16
+hGetContents' :: Handle -> IO String
+hGetContents' hdl = do
+  output  <- hGetContents hdl
+  _ <- evaluate $ length output
+  return output
+#endif
+
 -- Similar to System.Process.readCreateProcessWithExitCode, but stderr is
 -- inherited from the parent process, and output to stderr is not captured.
 readCreateProcessWithExitCode'
@@ -51,13 +60,19 @@ readCreateProcessWithExitCode' proc = do
         createProcess proc{ std_out = CreatePipe }
 
     -- fork off a thread to start consuming the output
-    output  <- hGetContents outh
     outMVar <- newEmptyMVar
-    _ <- forkIO $ evaluate (length output) >> putMVar outMVar ()
+    let onError :: SomeException -> IO ()
+        onError exc = putMVar outMVar (Left exc)
+    forkIO $ handle onError $ do
+      output <- hGetContents' outh
+      putMVar outMVar $ Right output
 
     -- wait on the output
-    takeMVar outMVar
+    result <- takeMVar outMVar
     hClose outh
+    output <- case result of
+      Left exc -> throwIO exc
+      Right output -> return output
 
     -- wait on the process
     ex <- waitForProcess pid



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1b7719bfa0c19f68e357cd0de7a742dd2d92d44c

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1b7719bfa0c19f68e357cd0de7a742dd2d92d44c
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200520/76c903f7/attachment-0001.html>


More information about the ghc-commits mailing list