[Haskell-cafe] timeout and waitForProcess

2007-11-20 Thread Tim Bauer

Does anyone know if the new System.Timeout.timeout combinator can wakeup from
System.Process.waitForProcess? I have not been able to make this work, and
it appears as though the timeout exception is thrown only after
waitForProcess returns normally (if it ever does). I am using Windows and
GHC 6.8.1.

import Control.Concurrent
import System.IO
import System.IO.Error(isEOFError)
import System.Process(runInteractiveProcess,
  waitForProcess,
  ProcessHandle)
import System.Timeout(timeout)

halfSecond = 50

example = timeout halfSecond sleepy

sleepy = do
  (inp,out,err,pid) - runInteractiveProcess sleep.exe [10] Nothing
Nothing
  forkOS (ioGobbler out)
  forkOS (ioGobbler err)
  waitForProcess pid  -- CAN timeout INTERRUPT THIS?
  return PROCESS FINISHED

ioGobbler :: Handle - IO ()
ioGobbler h = catch (ioDiscarder h) eofHandler

ioDiscarder :: Handle - IO ()
ioDiscarder h = do hGetChar h  ioDiscarder h

eofHandler :: IOError - IO ()
eofHandler e | isEOFError e   = return ()
 | otherwise  = ioError e


Executing `example' from ghci will spawn (sleep.exe 10) which sleeps for 10
seconds. The timeout should happen in half a second (giving the thread time
to get blocked into waitForProcess). However, instead, things block for ten
seconds (until sleep.exe exits and waitForProcess returns normally).

While a workaround is to spin and sleep with the getProcessExitCode and
threadDelay, it seems like an inappropriate hack. The documentation for
timeout lists some functions that are interruptible, but is not mean to be a
complete list.

Thanks all,
- Tim

-- 
View this message in context: 
http://www.nabble.com/timeout-and-waitForProcess-tf4847280.html#a13868823
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] timeout and waitForProcess

2007-11-20 Thread Bryan O'Sullivan

Tim Bauer wrote:

Does anyone know if the new System.Timeout.timeout combinator can wakeup from
System.Process.waitForProcess?


No, this is expected behaviour per the documentation:

The technique works very well for computations executing inside of the 
Haskell runtime system, but it doesn't work at all for non-Haskell code. 
Foreign function calls, for example, cannot be timed out with this 
combinator simply because an arbitrary C function cannot receive 
asynchronous exceptions.


In principle, this FFI restriction could be partly lifted on POSIX 
systems, at least for some library calls, by use of thread cancellation. 
 However, the thread cancellation rules are sufficiently subtle that I 
have never heard of anyone actually using this facility.  I wouldn't 
want to be trying to write or maintain this code, though.


b
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe