terminateProcess

Simon Marlow simonmarhaskell at gmail.com
Fri May 18 08:55:55 EDT 2007


Johannes Waldmann wrote:
> according to documentation of System.Process.terminateProcess,
> "This function should not be used under normal circumstances."
> Well, then what function then *should* be used normally?

You just shouldn't terminate processes, unless you know that the process you're 
terminating won't mind.  terminateProcess doesn't necessarily give the victim a 
chance to clean up - on Unix it gets a SIGTERM but on Windows it gets summarily 
executed (because there's no other way to terminate processes on Windows, 
AFAIK).  So if your process needs to clean up on exit, you shouldn't use 
terminateProcess.

> I have the following code in a multithreaded Haskell program,
> which does not work (if this thread is killed,
> the external process keeps running after the Haskell program exits)
> 
>         bracket
>             ( System.Process.runCommand cmd )
> 	    ( terminateProcess p	    )
> 	    ( polling_waitForProcess )
> 
> It took me a while to figure out why "waitForProcess" here
> does not work: Control.Concurrent.throwTo says
> "If the target thread is currently making a foreign call, then the
> exception will not be raised (and hence throwTo will not return
> until the call has completed.")

So you can work around this using an extra thread and an MVar: something like

   myWaitForProcess p = do
     m <- newEmptyMVar
     forkIO $ do r <- waitForProcess p; putMVar m r
     takeMVar m

as for why your code doesn't kill the process, I'm not exactly sure.  What 
platform?  If Unix, perhaps the process is ignoring SIGTERM?  You might have to 
wait a little while for the process to shutdown.  For example code that uses 
terminateProcess, you could look at

   http://darcs.haskell.org/testsuite/timeout/timeout.hs

Cheers,
	Simon


More information about the Libraries mailing list