Damian Nadales
Damian Nadales

Reputation: 5037

Reading from a handle obtained outside createProcess

I'm trying to create a process, and communicate with it via a handle that I provide outside the createProcess function:

stdOutH <- openFile (logDir </> "stdout.log") ReadWriteMode
hSetBuffering stdOutH LineBuffering
(_, _, _, ph) <- createProcess $
    (proc "someproc" []) { std_out = UseHandle stdOutH
                         , std_err = UseHandle stdErrH
                         }
line <- hGetLine stdOutH
putStrLn $ "Got " ++ line

The "someproc" process spits a line out to the standard output, and I want to read it from the process that spawned it. However if I try to do this I get the following error:

hGetLine: illegal operation (handle is closed)

What I don't understand is why the handle is closed while the created process is running. This works if I use CreatePipe instead of UseHandle, the problem is that I only want to read the first line. But doing this requires to keep on reading from the pipe, otherwise it gets full after a certain amount of output by "someproc".

So, is there a way to use system.process to communicate two processes via stdOutH in the way described above?

Upvotes: 3

Views: 121

Answers (1)

Yuras
Yuras

Reputation: 13876

This behavior of createProcess is documented:

Note that Handles provided for std_in, std_out, or std_err via the UseHandle constructor will be closed by calling this function.

Documentation suggests to use createProcess_ function instead.

Upvotes: 2

Related Questions