Skip to content

Process.WaitForExitAsync doesn't wait for the reditected output to be read  #42556

@meziantou

Description

@meziantou

Description

In #34294, @scalablecory said that Process.WaitForExitAsync is semantically equivalent to Process.WaitForExit. However, it seems that it is not the case when the process has exited before calling BeginOutputReadLine.

The following test succeeds. It is not expected as Assert.Empty(logs) should fail. Calling WaitForExit correctly raises the OutputDataReceived event, so the second assert succeeds as expected.

[Fact]
public async Task Test1()
{
    var logs = new List<string>();
    var psi = new ProcessStartInfo("cmd", "/C echo test")
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
    };
    using var process = new Process();
    process.StartInfo = psi;
    process.OutputDataReceived += (sender, e) => { if (e.Data != null) logs.Add(e.Data); };
    Assert.True(process.Start());

    // Give time for the process (cmd) to terminate
    Thread.Sleep(1000);

    process.BeginOutputReadLine();

    await process.WaitForExitAsync();
    Assert.Empty(logs); // The collection is empty, but it should contain 1 item

    process.WaitForExit();
    Assert.Equal(new[] { "test" }, logs); // ok
}

Configuration

Host (useful for support):
  Version: 5.0.0-rc.1.20451.14
  Commit:  38017c3935

.NET SDKs installed:
  3.1.300 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.7.20366.6 [C:\Program Files\dotnet\sdk]
  5.0.100-rc.1.20452.10 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.7.20365.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.1.20451.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.7.20364.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.1.20451.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.7.20366.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.1.20452.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions