
On Tue, Aug 26, 2008 at 1:35 PM, brian
One unworkable approach I tried is to get a lazy String from the handle with hGetContents. [...] The OP had the same problem I did, so he made a variant of hGetContents with timeout support. The problem: he used something from unsafe*. I came to Haskell for rigor and reliability and it would make me really sad to have to use a function with 'unsafe' in its name that has a lot of wacky caveats about inlining, etc.
unsafeInterleaveIO has no weird inlining caveats (as opposed to unsafePerformIO, which does). In fact, hGetContents is implemented using unsafeInterleaveIO; the source is here: http://haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-IO.html#hGetC... The "unsafe" bit is that it potentially allows side effects embedded within pure values; if the value is never demanded, the side effect never executes, so the observable behavior of your program can be made to depend on whether or not (or when) that value is evaluated. But when you are reading from a network stream, that's exactly what you want. -- ryan