2008/11/2 Rafal Kolanski <xs@xaph.net>

Unfortunately, it also segfaults once in a while, probably indicating I have some kind of race condition ... but I can't figure out why.

What is the "it" that segfaults? The Haskell program shouldn't, at least.

That said, your code for reading from child processes is wrong. You should read all of a child's output strictly (i.e. hGetContents alone will not do the trick) before you wait for its exit status. Your current scheme reverses this order, and there is hence no guarantee that it will read anything from the defunct child process. There's some possibility that this might be the cause of your segfault, which to me would suggest the presence of a bug in the runtime system.