Opened 5 years ago

Closed 5 years ago

#7583 closed bug (invalid)

IO reordering

Reported by: Heimdell Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.4.1
Keywords: IO, invalid evaluation order Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


I have a simple test program:

main = do putStr "> "
          line <- getLine
          putStrLn line

In ghci, it writes "> " before receiving input:

Prelude Main> main

But being compiled by ghc it writes "> " just after receiving input.

maelstrom@ubuntu:~/vault$ ./test 

That is not the behavior I expect.

Change History (2)

comment:1 Changed 5 years ago by Yuras

The evaluation order is correct. It is related to line buffering on stdout. You should flush stdout after writing "> " and before reading input.

In ghci the behavior is different probably cos it setup different buffering on stdout.

comment:2 Changed 5 years ago by monoidal

Resolution: invalid
Status: newclosed

This is because GHC uses line buffering by default, while GHCi disables buffering. You can flush after each output or disable buffering:

import System.IO

main = do hSetBuffering stdout NoBuffering
          putStr "> "
          line <- getLine
          putStrLn line
Note: See TracTickets for help on using tickets.