dotnet/runtime

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

Open

#91.891 geöffnet am 11. Sept. 2023

Auf GitHub ansehen
 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (5.445 Forks)batch import
area-System.Diagnostics.Processhelp wanted

Repository-Metriken

Stars
 (17.886 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)

Beschreibung

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();

Contributor Guide