process
process copied to clipboard
Error when using `interruptProcessGroupOf` on Mac OS
Running the code below in Ubuntu I got
$ ./Test
The Glorious Glasgow Haskell Compilation System, version 8.0.1
Just ExitSuccess
but in Mac OS my student @jonaprieto got
$ ./Test
The Glorious Glasgow Haskell Compilation System, version 8.0.1
Test.hs: getProcessGroupIDOf: does not exist (No such process)
The error disappears if I replace interruptProcessGroupOf by terminateProcess.
$ cat Test.hs
module Main where
import Data.Maybe
import System.IO
import System.Process
foo :: IO (ProcessHandle, String)
foo = do
(_, oh, _, ph) <-
createProcess CreateProcess
{ cmdspec = RawCommand "ghc" ["-V"]
, cwd = Nothing
, env = Nothing
, std_in = Inherit
, std_out = CreatePipe
, std_err = Inherit
, close_fds = False
, create_group = True
, delegate_ctlc = False
, create_new_console = False
, detach_console = False
, new_session = False
, child_group = Nothing
, child_user = Nothing
}
o <- hGetContents $ fromMaybe (error "foo") oh
return (ph, o)
main :: IO ()
main = do
(ph, o) <- foo
putStr o
_ <- interruptProcessGroupOf ph
e <- getProcessExitCode ph
print e
The issue is that the process has already exited by the time getProcessGroupIDOf is called, and therefore the call fails. I don't know why that call does not fail on Linux. Perhaps reasonable behavior would be to simply swallow that exception and perform a no-op, on the assumption that if getProcessGroupIDOf fails, it's because the process has already exited.
@simonmar It looks like you last touched the getProcessGroupIDOf call, how do you feel about that change?