Posix IO & Unicode lookup exception

Hi, I wanted to see if anyone had an idea about why the following program results in an exception. The issue seems to be related to stdin to a POSIX file descriptors for a subprocess, and GHC's Unicode encoding lookup. I've tried to make the program as simple as possible to make the issue obvious. Consider:
module Pipe where
import System.Process import System.IO import System.Posix.IO
main = sequence_ $ replicate 100000 f
f = do (Just hin, _, _, prc) <- createProcess (shell "./pipe_c.out") { std_in = CreatePipe , std_out = UseHandle stdout , std_err = UseHandle stderr}
pin <- handleToFd hin _ <- fdWrite pin ("h\n") closeFd pin waitForProcess prc
and a trivial C program to read in the data (but doing nothing with it):
---------------------------------------
#include
gcc -Wall -o pipe_c.out pipe_c.c ghci Pipe.hs main ... *** Exception: malloc: resource exhausted (out of memory) iconvOpen ("UTF-8","UTF-8"): resource exhausted (Cannot allocate memory)
My best guess is that the GHC is calling iconv_open() http://www.kernel.org/doc/man-pages/online/pages/man3/iconv_open.3.html, but not making a subsequent call to iconv_close() http://www.kernel.org/doc/man-pages/online/pages/man3/iconv_close.3.html. (Actually, my best guess is I'm making a silly mistake, but I'm having trouble seeing it. :) ) I am running 2.6.35-28-generic (Ubuntu) and GHC 7.0.2, but I've observed the same issues with GHC 6.12.3. Lee

My guess is that you are closing the file descriptor out from under the handle. It is probably the handle that has the open iconv connection, and since it is never closed, you run out of memory. Why not just interact with the handle? Use hPutStr instead of fdWrite, and hClose instead of closeFd. - Mark
participants (2)
-
Lee Pike
-
Mark Lentczner