[GHC] #13440: putStr doesn't have the same behavior in GHCi and after compilation with GHC

GHC ghc-devs at haskell.org
Fri Mar 17 23:19:33 UTC 2017


#13440: putStr doesn't have the same behavior in GHCi and after compilation with
GHC
-------------------------------------+-------------------------------------
        Reporter:  vanto             |                Owner:  (none)
            Type:  bug               |               Status:  closed
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.2
      Resolution:  duplicate         |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  Other             |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:  #2189             |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by RyanGlScott):

 * status:  new => closed
 * resolution:   => duplicate
 * related:   => #2189


Comment:

 As it turns out, this is a duplicate of a //long//-standing bug in GHCi,
 #2189. Fixing that bug will probably require rewriting the whole IO
 manager to use native Win32 IO (see #11394), but luckily, someone is
 working on this.

 Until then, I can offer you two workarounds.

 1. If you want to have a stronger guarantee that `"hello"` will be printed
 first, you can use `hFlush stdout` to force this:

 {{{#!hs
 import System.IO

 main :: IO ()
 main = do {
              putStr "hello";
              hFlush stdout;
              x <- getChar; -- or x <- getLine;
              putStr "x = ";
              print x;
            }
 }}}

 2. Alternatively, you can try a different buffering strategy. By default,
 `stdout`'s buffering mode is `NoBuffering` (which should, in theory, mean
 that all output is immediately printed to the screen, were it not for
 #2189). But you can change the buffering mode to something else:

 {{{#!hs
 import System.IO

 main :: IO ()
 main = do {
              hSetBuffering stdout $ BlockBuffering $ Just 1;
              putStr "hello";
              x <- getChar; -- or x <- getLine;
              putStr "x = ";
              print x;
            }
 }}}

    This does buffer the output, but only 1 character at a time.

 I've experimentally verified that both of these workaround work on my
 Windows machine, on both `cmd.exe` and MSYS2.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13440#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list