Control.Concurrent.Async.race and runInteractiveProcess

I am using the race function from the async package exported by Control.Concurrent.Async .

The subtasks that I run using race themselves call runInteractiveProcess to run executable files (non-Haskell). The idea is to run various external programs and execute the result of the first. In a sense, Haskell "organizes" a bunch of external programs.

What I am observing is that while the race working correctly, killing the Haskell level slow flow level; subprocesses generated from the slowest thread continue to work.

I suspect that waiting for a race to kill processes spawned in this way is a little unrealistic, as they probably become zombies and are inherited by init. However, for my purposes, maintaining external processes leads to victory in the goal of using race in the first place.

Is there an alternative way to use race so that subprocesses created this way are also killed? Although I don't have a use case yet, it would be better if the entire process chain created from race d tasks was killed; since you can imagine that these external programs themselves create many processes.

+8
haskell
source share
1 answer

As mentioned in the comments, you can use a combination of onException and terminateProcess .

My process-streaming library (which contains helper functions built on top of process and pipes ) already does this. Asynchronous exceptions trigger termination of the external process.

For example, the following code does not create the toolate.txt file.

 import qualified Pipes.ByteString as B import System.Process.Streaming import Control.Concurrent.Async main :: IO () main = do result <- race (runProgram prog1) (runProgram prog2) putStrLn $ show $ result where -- collecting stdout and stderr as bytestrings runProgram = simpleSafeExecute $ pipeoe $ separated (surely B.toLazyM) (surely B.toLazyM) prog1 = shell "{ echo aaa ; sleep 2 ; }" prog2 = shell "{ echo bbb ; sleep 7 ; touch toolate.txt ; }" 

Result:

 Left (Right ("aaa\n","")) 
+5
source share

All Articles