How do I keep a spawned process alive in Haskell?

Viewed 132

I'm trying to set up a "bridge" between my Haskell code and an interactive command-line process. More specifically, I'm trying to run an Elm REPL and send/receive through stdin/stdout. I wasn't sure exactly which library to use for this, but I went with typed-process.

The issue I have is that my Haskell program finishes (or quits) while the REPL process is still running. How do I avoid this?

Also, another problem is that the REPL process isn't getting any input from the stdin handle.

My current code looks like this:

run :: Document -> IO (Result () Text)
run (Document moduleName tests) =  do
    let config = createConfig

    p <- startProcess config

    hSetBuffering (getStdin p) NoBuffering
    hSetBuffering (getStdout p) NoBuffering

    Data.Text.IO.hPutStr (getStdin p) "True\n"
    Data.Text.IO.hGetChunk (getStdout p) >>= print

    _ <- waitExitCode p

    return (Ok ())


{-| Config for process.
-}
createConfig =
    shell "elm repl"
        |> setStdin createPipe
        |> setStdout createPipe
        |> setStderr closed
1 Answers

From the docs, it seems that stopProcess forces the process to stop (it sends SIGTERM on unix). This is because the docs state that it calls terminateProcess, and then waits.

We only want to wait without terminating the process. I would try waitExitCode or similar functions, instead.

Related