[Haskell-cafe] Re: exitFailure under forkProcess

Andrew Pimlott andrew at pimlott.net
Tue Oct 26 22:57:10 EDT 2004

On Wed, Oct 27, 2004 at 12:56:12AM +0000, John Goerzen wrote:
> If you follow this a little bit, you'll find that forkProcess is *NOT*
> throwing the exception that is being reported here.  The message is
> being printed by the RTS of the child process.  No exception is thrown
> in the parent.  (Believe me, I've tried to catch it!)

I don't have any idea what's happening here, except that it semes an
obvious bug.  I found that a work-around is to use exitImmediately in
the child.  This is often advisable after a fork anyway, because you
don't want finalizers or atexit hooks to run in the child.  Presumably,
it is some such code causing the breakage, though I can't imagine how.

On Wed, Oct 27, 2004 at 01:02:52AM +0000, John Goerzen wrote:
> Yes.  Its POSIX interface is, uhm, weird.  I can't quite put my finger
> on it, but things like setting up a pipe to a child process's stdin just
> seem brittle and fragile with all sorts of weird errors.  I can do this
> in my sleep in C, Perl, or Python but in Haskell I can barely make it
> work when I'm fully conscious :-)

Actually, your bug aside, and despite minimal documentation, I've found
the Posix modules straightforward.  I wrote a version of system that
returns the output of the command as a string, and it worked on the
first try!  I'm sure there are better versions around, but:

system :: [String] -> IO String     
system (cmd:args) = do
  (read, write) <- createPipe
  pid           <- forkProcess (closeFd read >> child write)
  closeFd write
  ret           <- fdToHandle read >>= hGetContents
  length ret `seq` getProcessStatus True False pid
  return ret
  child write = do
    dupTo write stdOutput
    closeFd write
    executeFile cmd
                True  -- path
                Nothing  -- env


More information about the Haskell-Cafe mailing list