haskellbufferingxinetd

Haskell default io buffering


Yesterday I wrote a little xinetd exercise for my students: make a reverse echo program.

To learn something new, I tried to implement a Haskell solution. The trivial main = forever $ interact reverse does not work. I went through this question and made a corrected version:

import Control.Monad
import System.IO
 
main = forever $ interact revLines
 
revLines = unlines . map (reverse) . lines 

But this corrected version also doesn't work. I read the buffering documentation and played with the various settings. If I set NoBuffering or LineBuffering, my program works correctly. Finally I printed out the default buffering modes for stdin and stdout.

import System.IO

main = do 
  hGetBuffering stdin >>= print 
  hGetBuffering stdout >>= print

I've got BlockBuffering Nothing if I run my program from xinetd(echo "test" | nc localhost 7) but from cli I've got LineBuffering


Edit: Thank you all for the helpful answers.

I accept the answer which was given by blaze, he give me a hint with isatty(3). I went through again the System.IO documentation and found the hIsTerminalDevice function, with which I am able to check the connection of the handle.

For the record, here is my final program:

{-# OPTIONS_GHC -W #-}

import System.IO
 
main = do
  hSetBuffering stdin LineBuffering
  hSetBuffering stdout LineBuffering
  
  interact revLines
 
revLines = unlines . map (reverse) . lines 

Solution

  • It's not specific to Haskell (e.g. the standard C library does the same thing). Traditionally, if a file descriptor corresponds to the terminal, buffering is set to line mode, otherwise to block mode. File descriptor type can be checked by the isatty(3) function -- not sure if it is exported to System.IO.

    And yes, you need to set buffering mode manually if you depend on it.

    By the way, you can cheat the system and force block buffering in the command line by running your program as cat | ./prog | cat.