dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.6k stars 4.56k forks source link

Process.StandardOutput.Peek() and Process.StandardError.Peek() hang until StreamReader receives data. #91891

Open ezverev opened 10 months ago

ezverev commented 10 months ago
          Closing this issue because I cannot reproduce it reliably.

Originally posted by @jscarle in https://github.com/dotnet/runtime/issues/74677#issuecomment-1228951480

The original issue suggested to repro the problem using the Process.StandardOutput which is suboptimal because most of the processes we usually run produce some output. The issue shows itself only when output is empty. Just try the same but with the Process.StandardError. In most of the cases there will be no data in that stream and the issue will reproduce.

    var process = Process.Start(new ProcessStartInfo("path\to\executable.exe", arguments)
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        StandardOutputEncoding = Encoding.UTF8,
        StandardErrorEncoding = Encoding.UTF8
    });

    var outputBuilder = new StringBuilder();
    while (process.StandardError.Peek() > -1)
        outputBuilder.Append((char)process.StandardError.Read());
    var result = outputBuilder.ToString();
ghost commented 10 months ago

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process See info in area-owners.md if you want to be subscribed.

Issue Details
Closing this issue because I cannot reproduce it reliably. _Originally posted by @jscarle in https://github.com/dotnet/runtime/issues/74677#issuecomment-1228951480_ The original issue suggested to repro the problem with the StandardOutput StreamReader which is suboptimal because most of the processes we usually run produce some output. The issue demands that the output is empty. Just try the same but with the StandardError StreamReader. With most of the cases you might take there will be no data in that stream and the issue will reproduce. ``` var process = Process.Start(new ProcessStartInfo("path\to\executable.exe", arguments) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 }); var outputBuilder = new StringBuilder(); while (process.StandardError.Peek() > -1) outputBuilder.Append((char)process.StandardError.Read()); var result = outputBuilder.ToString(); ```
Author: ezverev
Assignees: -
Labels: `area-System.Diagnostics.Process`
Milestone: -
AugustoDeveloper commented 3 months ago

I had same issue on linux

cartercanedy commented 3 months ago

any updates with this?