Handle close on GC leading to wrong reported exit codes?

Niklas Hambüchen <mail <at> nh2.me>
2012-08-24 14:10:00 GMT

According to System.IO, handles are automatically closed once they are
garbage-collected.
This served me well for files so far, but for processes, it gives me a
weird problem: If I don't prevent my stdErr handle from being
garbage-collected, the exit code reported by getProcessExitCode is just
wrong (for me, it's always ExitSuccess, no matter what the program does).
I posted this into #haskell and some poeple could not reproduce it, so I
wanted to ask the Cafe what happens if you run this and if you have an
idea why it happens to me.
Thank you
Niklas
The following code gives an example.
(http://hpaste.org/73593)
You can see the real exit code with "./test; echo $?".
module Main where
import Control.Concurrent (threadDelay)
import System.Process
import System.IO
import System.Exit
run :: FilePath -> [String] -> IO (Handle, Handle, Handle, ProcessHandle)
run cmd args = do
r <at> (i, o, e, p) <- runInteractiveProcess cmd args Nothing Nothing
getProcessExitCode p >>= \me -> case me of