fsprojects / FAKE

FAKE - F# Make
https://fake.build
Other
1.28k stars 581 forks source link

races in console trace while running Shell.Exec #2653

Open zhongjn opened 2 years ago

zhongjn commented 2 years ago

Description

While running commands with Shell.Exec, output from stdout or stderr gets wrong.

Repro steps

  1. Step A Prepare a bash script err.sh:

    >&2 echo err1 # writes to stderr
    >&1 echo out1 # writes to stdout
    >&2 echo err2
    >&1 echo out2
    sleep 1
  2. Step B Use Shell.Exec to execute the bash script. I executed the script multiple times here to make the problem more obvious.

    Shell.Exec("bash", "scripts/err.sh") |> ignore
    Shell.Exec("bash", "scripts/err.sh") |> ignore
    Shell.Exec("bash", "scripts/err.sh") |> ignore

Expected behavior

Output are colored accordingly. Lines are not messed up.

Actual behavior

image

Known workarounds

Please provide a description of any known workarounds.

Related information

github-actions[bot] commented 2 years ago

Welcome to the FAKE community! Thank you so much for creating your first issue and therefore improving the project!

zhongjn commented 2 years ago

The root cause shold be:

  1. While running the command, callbacks are executed in different threads:

https://github.com/fsprojects/FAKE/blob/79aa1273d0311595bd404be5721d994301774552/src/app/Fake.Core.Process/Process.fs#L843-L850

  1. Trace is not thread-safe, since ConsoleTraceListener is not thread-safe:

https://github.com/fsprojects/FAKE/blob/79aa1273d0311595bd404be5721d994301774552/src/app/Fake.Core.Trace/TraceListener.fs#L273-L308

Could we simply make ConsoleTraceListener thread-safe to fix this problem? If so, i could make a PR to fix this.

yazeedobaid commented 2 years ago

Thanks for reporting. Can you please use the create process API instead? Since the asyncShellExex is marked with an obsolete attribute. https://github.com/fsprojects/FAKE/blob/79aa1273d0311595bd404be5721d994301774552/src/app/Fake.Core.Process/Process.fs#L826-L829