ghc-6 messing with terminal settings?

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Fri Jul 2 05:13:00 EDT 2004


OK, here's a strange bug.  I have a program (hat-trail) that uses
System.system("stty -icanon -echo") together with resetting the
buffering of I/O, in order to get so-called character-break mode on the
terminal.  Once the program is finished, it calls System.system("stty
icanon echo") to restore the terminal to normal.  Under ghc-5.x and
nhc98, this works just fine.  But under ghc-6.x, the terminal remains
in character mode as if the final reset had not taken place.

Having investigated somewhat (using strace to determine the order of
system calls), it turns out that ghc-6 is very sensitive to the order
in which operations are called.  Here is a cut-down demo program:

    import System; import Char; import IO
    main = do system ("stty -icanon -echo")
              hSetBuffering stdin NoBuffering
              hSetBuffering stdout NoBuffering
              getChars
              system ("stty icanon echo")
              putStrLn "Done"
    getChars = do c <- getChar
                  putChar (toUpper c)
                  if c=='q' then return () else getChars

Running it leaves the terminal in char mode.  But if you swap the
order of operations so that the call to stty comes *after* setting
the buffering, the program then works correctly.

It would appear that ghc-6 is being careful to save the current state
of the terminal at the point of hSetBuffering, and to restore it on
exit, something ghc-5 did not do.  But paradoxically, this "safer"
choice caused things to mess up, instead of getting better!

Regards,
    Malcolm




More information about the Glasgow-haskell-bugs mailing list