title: STDIN stopps when piping from a git bash window to a .NET application
body: <p>I have a Windows c#/.Net application that I am trying to use to take binary standard input and do some processing on it.  However, for some reason the standard input "stops" and the read operation hangs when I invoke it using a git bash window.</p>
  
  <p>My program is called <code>StdioSandbox</code> and I'm using the yes command as a test case (cat did the same thing with a large binary file).  Here is the command:</p>
  
  <p><code>$ yes | ./StdioSandbox.exe</code></p>
  
  <p>Here is the code for StdioSandbox:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace StdioSandbox
  {
     class Program
     {
        static ManualResetEvent _inputDone;
        static Stream _inputStream;
        static byte[] _inputBuffer;
        static readonly int BUFFER_SZ = 65000;
        static void Main(string[] args)
        {
           _inputDone = new ManualResetEvent(false);
           _inputStream = Console.OpenStandardInput(BUFFER_SZ);
           _inputBuffer = new byte[BUFFER_SZ];
  
           BeginRead();
  
           _inputDone.WaitOne(-1);
  
           Console.Write("Press any key to continue...");
           Console.ReadKey(true);
  
        }
  
        static void BeginRead()
        {
           Console.WriteLine("BeginRead");
           _inputStream.BeginRead(_inputBuffer, 0, _inputBuffer.Length, EndReading, null);
        }
  
        static void EndReading(IAsyncResult ar)
        {
           Console.WriteLine("EndRead");
           int byteCount = _inputStream.EndRead(ar);
           Console.WriteLine($"Read in {byteCount} bytes from STDIN");
           if (0 == byteCount)
           {
              Console.WriteLine("Input stream has ended");
              _inputDone.Set();
           }
           else
           {
              BeginRead();
           }
        }
     }
  }
  </code></pre>
  
  <p>When I run this I expect an endless stream of output as it continues to read from STDIN but after a short burst it just stops and appears to be blocking on the BeginRead call.</p>
  
  <pre class="lang-none prettyprint-override"><code>...
  BeginRead
  EndRead
  Read in 8192 bytes from STDIN
  BeginRead
  EndRead
  Read in 7168 bytes from STDIN
  BeginRead
  EndRead
  Read in 4096 bytes from STDIN
  BeginRead
  EndRead
  Read in 6144 bytes from STDIN
  BeginRead
  EndRead
  Read in 7168 bytes from STDIN
  BeginRead
  EndRead
  Read in 10240 bytes from STDIN
  BeginRead
  EndRead
  Read in 5120 bytes from STDIN
  BeginRead
  [hung]
  </code></pre>
  
  <p>If I run the exact same stuff in a windows cmd.exe terminal it works fine (keeps going until I stop it with CTRL+C).  Here is the Windows cmd.exe terminal command line:</p>
  
  <p><code>"c:\Program Files\Git\usr\bin\yes.exe" | StdioSandbox.exe</code></p>
  
  <p>Also, oddly, if I add a <code>Sleep(1)</code> it <em>will</em> continue to read in bash but that is not ideal because I'm trying to process input quickly.</p>
  
  <p>What am I missing here?  Is there a fundamental IO difference that I'm not accounting for?</p>