Skip to content

Unpredictable ordering of native command output when merging the error stream into the success output stream #5424

@mklement0

Description

@mklement0

Steps to reproduce

bash -c 'echo a; echo b >&2; echo c' 2>&1

On Windows, use cmd /c 'echo a & echo b >&2 & echo c' 2>&1.
Additionally, in Windows PowerShell, append | % tostring.

Note that it is the addition of 2>&1 that triggers the problem.

Expected behavior

a
b
c

That is, the output lines should be printed in the order written, across streams.

Actual behavior

Intermittently, the output order is unexpected in that the stderr output line is printed out of order; e.g.:

a
c
b

or

b
a
c

If you have trouble seeing the symptom, try something like the following

Unix:

while ($true) { if (($a = bash -c 'echo a; echo b >&2; echo c' 2>&1)[1] -notmatch 'b') { $a; break} }

Windows:

while ($true) { if (($a = cmd /c 'echo a & echo b >&2 & echo c' 2>&1 | % tostring)[1] -notmatch 'b') { $a; break}  }

Environment data

PowerShell Core v6.0.0-beta.9 on macOS 10.13
PowerShell Core v6.0.0-beta.9 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions