.NET 6 reading standard output blocks on Linux

Viewed 18

I have a .NET 6 process prog1 which starts a .NET 6 process prog2, redirects its standard output and waits for all output. prog2 starts a .NET 6 process prog3 without redirecting its output and shuts down. prog3 runs forever.

The code looks like this:

// prog1:
using System.Diagnostics;

var process = new Process
{
    StartInfo =
    {
        FileName = "prog2",
        RedirectStandardOutput = true,
        UseShellExecute = false
    }
};
process.Start();
process.WaitForExit();

Console.WriteLine(process.StandardOutput.ReadToEnd());

// prog2:
using System.Diagnostics;

using var process = new Process
{
    StartInfo =
    {
        FileName = "prog3",
        UseShellExecute = true
    }
};
process.Start();

// prog3:
Thread.Sleep(Timeout.Infinite);

This works just fine on Windows, but on Linux prog1 hangs forever in the line

Console.WriteLine(process.StandardOutput.ReadToEnd());

and waits for prog3 to finish.

AFAIK .NET does already duplication of stdout file descriptors after the fork, so prog3 should not affect prog1. Is this a bug? Or am I missing something in my implementation?

0 Answers
Related