[Haskell-cafe] catting to cat gets stuck at > 135K

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Mon Nov 10 16:37:48 EST 2008

On 2008 Nov 10, at 16:29, Jason Dusek wrote:
>  I've put together a simple test case for a rather annoying
>  problem. I've got a program that drives other programs. For
>  example, it can drive `cat`:
> :; Simple cat a-file
>  When the file is a little bit greater than 135060 bytes, this
>  program fails to produce any output at all -- I need to use ^C
>  to get my console back.

If you are feeding it input and collecting output, you need to forkIO  
one or both.  In particular, the sequence

	feed input
	read output

will deadlock if at any point the input or output pipe fills:  one  
side will be blocked on write() waiting for the other side to read()  
from the pipe, while the read() side is blocked waiting for write() on  
the other pipe, which won't be read() because that's the first side.

(There are other variants of this, but that's the general form:   
processes blocked each waiting for the other side to do something.)

>  If I remove the `hClose`, the example program just hangs, no
>  matter the size of the input.

That's another symptom of it, yes, made worse because the pipe close  
is now implicit and the other side won't stop read()ing until the pipe  
is close()d by the first side.

> simple exe bytes args        =  do
>    (i, o, e, p)            <-  runInteractiveProcess exe args  
> Nothing Nothing
>    hPut i bytes
>    s                       <-  hGetContents o
>    hClose i
>    return s

Yep, that's your problem.  forkIO the hPut.

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Haskell-Cafe mailing list