Alfonso Acosta | 1 Aug 16:13

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:
(Continue reading)

Alfonso Acosta | 1 Aug 16:20

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

In case it helps, I have just confirmed the same behaviour under Windows.
Bayley, Alistair | 1 Aug 16:25

RE: runCommand/waitForProcess don't respect text printing order whenstdout is redirected

> From: libraries-bounces <at> haskell.org 
> [mailto:libraries-bounces <at> 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.
*****************************************************************
Alfonso Acosta | 1 Aug 16:32

Re: runCommand/waitForProcess don't respect text printing order whenstdout is redirected

On Fri, Aug 1, 2008 at 4:25 PM, Bayley, Alistair
<Alistair.Bayley <at> invesco.com> wrote:
> 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.
Johannes Waldmann | 1 Aug 17:51
Favicon

Re: runCommand/waitForProcess don't respect text printing order whenstdout is redirected


>>  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.
Henning Thielemann | 5 Aug 13:04

Re: runCommand/waitForProcess don't respect text printing order whenstdout is redirected


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.

Gmane