#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 Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

I have a simple test program:

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

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

Prelude Main> main
> WHAT
WHAT

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

maelstrom@ubuntu:~/vault$ ./test 
WHAT
> WHAT

That is not the behavior I expect.

Change History (2)

comment:1 Changed 15 months 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 15 months ago by monoidal

  • Resolution set to invalid
  • Status changed from new to closed

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.