runCommand/waitForProcess don't respect text printing order when stdout is redirected

Hi all, I'm running ghc 6.8.2 in OSX and Linux. The following program behaves as expected when run on a terminal. == module Main where import System.Process main = sequence $ replicate 3 command where command = do putStrLn "foo" waitForProcess =<< runCommand "echo echo" putStrLn "bar" == $ ghc --make Main.hs -o main $./main foo echo bar foo echo bar foo echo bar However, when stdout is redirected to a file, the order is no longer respected: $ ./main > output $ cat output echo echo echo foo bar foo bar foo bar Am I missing something or should I file a bug report? Thanks in advance, Fons

In case it helps, I have just confirmed the same behaviour under Windows.

From: libraries-bounces@haskell.org [mailto:libraries-bounces@haskell.org] On Behalf Of Alfonso Acosta
The following program behaves as expected when run on a terminal.
However, when stdout is redirected to a file, the order is no longer respected:
Possibly buffering? I think the terminal has line buffering by default, whereas files are usually block-buffered. Try changing the buffering to line: hSetBuffering stdout LineBuffering Alistair ***************************************************************** Confidentiality Note: The information contained in this message, and any attachments, may contain confidential and/or privileged material. It is intended solely for the person(s) or entity to which it is addressed. Any review, retransmission, dissemination, or taking of any action in reliance upon this information by persons or entities other than the intended recipient(s) is prohibited. If you received this in error, please contact the sender and delete the material from any computer. *****************************************************************

On Fri, Aug 1, 2008 at 4:25 PM, Bayley, Alistair
Possibly buffering? I think the terminal has line buffering by default, whereas files are usually block-buffered. Try changing the buffering to line:
hSetBuffering stdout LineBuffering
Thanks! That worked. Sorry for the noise. I assumend that files, just like terminals, where line-buffered.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
hSetBuffering stdout LineBuffering
I run into this from time to time as well. I think the ghc (runtime) runtime behaviour is confusing here, as it tries to be too clever. I don't think it should even try to find out whether it's writing to terminal or file. Best regards, J.W. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iEYEARECAAYFAkiTMRkACgkQDqiTJ5Q4dm8MRACfev267AhVPuxkDtCNP6Wz0G/n CLMAn1RqRS2qRtlDnnzneF2M9kr2QoiR =pi+1 -----END PGP SIGNATURE-----

On Fri, 1 Aug 2008, Johannes Waldmann wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
hSetBuffering stdout LineBuffering
I run into this from time to time as well. I think the ghc (runtime) runtime behaviour is confusing here, as it tries to be too clever. I don't think it should even try to find out whether it's writing to terminal or file.
As far as I know it's the same behaviour as that of C standard libraries.
participants (4)
-
Alfonso Acosta
-
Bayley, Alistair
-
Henning Thielemann
-
Johannes Waldmann