Skip to content

StreamReader.ReadLineAsync() blocks on empty stdin in .NET 7 RC 2 #77358

Closed
@k15tfu

Description

@k15tfu

Hi!

I just faced with the problem that after upgrading to .NET 7 RC 2 the following program hangs forever, but it successfully exits with a timeout in .NET 6:

using System;
using System.IO;
using System.Threading;

namespace readline_async_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("starting...");

            var cts = new CancellationTokenSource();
            cts.CancelAfter(3000);

            Console.WriteLine("cts will be cancelled after 3sec...");
            TextReader reader = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding);
            var readTask = reader.ReadLineAsync();

            Console.WriteLine("waiting...");
            try
            {
                readTask.Wait(cts.Token);
            }
            catch (System.OperationCanceledException)
            {
                // nothing
            }

            Console.WriteLine("exiting...");
        }
    }
}

In .NET 6.0.10 the output is:

.../dotnet-sdk-6.0.402-linux-x64$ ./dotnet .../readline_async_csharp/bin/Debug/net6.0/readline_async_csharp.dll
starting...
cts will be cancelled after 3sec...
waiting...
exiting...

But in .NET 7.0.0-rc.2 the output is:

.../dotnet-sdk-7.0.100-rc.2.22477.23-linux-x64$ ./dotnet .../readline_async_csharp/bin/Debug/net7.0/readline_async_csharp.dll
starting...
cts will be cancelled after 3sec...
(still running)

and at this time the main thread is blocked here:

Main Thread @28606
	Interop.Sys.ReadStdin()
	StdInReader.ReadKey()
	StdInReader.ReadLineCore()
	StdInReader.ReadLine()
	ConsoleStream.ReadAsync()
	StreamReader.<ReadBufferAsync>d__72.MoveNext()
	AsyncMethodBuilderCore.Start<System.IO.StreamReader.<ReadBufferAsync>d__72>()
	StreamReader.ReadBufferAsync()
	StreamReader.<ReadLineAsyncInternal>d__63.MoveNext()
	AsyncMethodBuilderCore.Start<System.IO.StreamReader.<ReadLineAsyncInternal>d__63>()
	StreamReader.ReadLineAsyncInternal()
	StreamReader.ReadLineAsync()
	StreamReader.ReadLineAsync()
	Program.Main() at .../readline_async_csharp/Program.cs:line 18

readline_async_csharp.tar.gz

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions