
mrd on 2007-01-19 18:16:31 -0500:
In GHC 6.6 (and HEAD), it seems to block unexpectedly, and this strange behavior is related to hIsOpen and hIsEOF, somehow. It acts like hIsOpen or hIsEOF is holding up the loop until input arrives, but it still blocks at hGetLine.
The server ends up printing out lines that were typed into the client a while ago, as if it were stuck on some kind of backlog but could not catch up.
[...]
import Control.Concurrent import Control.Concurrent.STM import System.IO import Network
main = withSocketsDo $ do sock <- listenOn $ PortNumber 9000 (h,_,_) <- accept sock
From your description it sounds like you have a buffering problem. Have you tried adding 'hSetBuffering h LineBuffering' here?
ch <- atomically newTChan -- thread which pumps lines read -- from socket into channel forkIO . sequence_ . repeat $ hGetLine h >>= atomically . writeTChan ch -- main thread just waits for data from channel -- and prints it to screen sequence_ . repeat $ do isOpen <- hIsOpen h isEOF <- hIsEOF h putStrLn $ "handle isOpen = " ++ show isOpen ++ "; isEOF = " ++ show isEOF hFlush stdout line <- atomically $ readTChan ch putStrLn line hFlush stdout