Question

I am trying to write a Haskell program that executes an interactive program (also written in Haskell), sending and receiving lines of text. The interactive program reads from stdIn and writes to stdOut using the standard Haskell library.

However, this proved to be more complicated than I anticipated, probably due to Haskell lazyness or some other misterious thing happening on the background. The program apparently goes into deadlock, and does not receive back the line of text it was expected to receive. It does receive the text fine if the interactive program terminates as a result of sending the line of text, but I need the program to keep running and receive more data (it's called interactive for a reason). Sometimes it prints the expected output only after I kill the program receiving the message. The code looks like this:

main = do
    (hin,hout,herr,pl) <- (runInteractiveCommand "./PlayerMain")
    hSetBinaryMode hin False
    hSetBinaryMode hout False
    hSetBuffering hin LineBuffering
    hSetBuffering hout NoBuffering
    hPutStr hin "start\n"
    out <- hGetLine hout
    putStrLn out

I already tried replacing lazy strings with strict Data.Text, but the behaviour was the same. Any light?

Was it helpful?

Solution

Maybe the reason is that the output of ./PlayerMain is being buffered. ./PlayerMain writes something that doesn't get flushed right away, and then hangs because it keeps expecting more input.

That would explain why you do get the message when the program terminates.

So maybe it will work if you modify ./PlayerMain and set its stdout to NoBuffering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top