Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Last active July 6, 2016 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-cooper/95bdf602c410ec7ebb531b61c8b91be2 to your computer and use it in GitHub Desktop.
Save charles-cooper/95bdf602c410ec7ebb531b61c8b91be2 to your computer and use it in GitHub Desktop.
waitForProcess bug
module Main where
import System.Process
import Control.Concurrent
main :: IO ()
main = do
(_, _, _, p) <- createProcess ((shell "sleep 1") { create_group = True })
forkIO $ waitForProcess p >>= print
threadDelay 10
-- interruptProcessGroupOf p
waitForProcess p >>= print
putStrLn "Success!"
-- 'Safe' version
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import System.Process
import Control.Concurrent
import Control.Exception
import System.Exit
waitForProcessSafe :: ProcessHandle -> IO ExitCode
waitForProcessSafe p = waitForProcess p
`catch` (\(e :: IOError) -> do
threadDelay 1 -- give the other thread enough time to acquire the lock
waitForProcess p)
main :: IO ()
main = do
(_, _, _, p) <- createProcess ((shell "sleep 1") { create_group = True })
forkIO $ waitForProcessSafe p >>= print
threadDelay 10
-- interruptProcessGroupOf p
waitForProcessSafe p >>= print
putStrLn "Success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment