Cancel NetworkStream.ReadAsync with TcpListener

Consider the following simplified example (ready to transfer to LinqPad, an elevated account is required):

void Main()
{
    Go();
    Thread.Sleep(100000);
}
async void Go()
{
    TcpListener listener = new TcpListener(IPAddress.Any, 6666);
    try
    {
        cts.Token.Register(() => Console.WriteLine("Token was canceled"));
        listener.Start();
        using(TcpClient client = await listener.AcceptTcpClientAsync()
                                               .ConfigureAwait(false))
        using(var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
        {
            var stream=client.GetStream();
            var buffer=new byte[64];
            try
            {
                var amtRead = await stream.ReadAsync(buffer,
                                                     0,
                                                     buffer.Length,
                                                     cts.Token);
                Console.WriteLine("finished");
            }
            catch(TaskCanceledException)
            {
                Console.WriteLine("boom");
            }
        }
    }
    finally
    {
        listener.Stop();
    }
}

If I connect the telnet client to localhost:6666and sit idle for 5 seconds, why do I see that the “token was canceled”, but never saw the “boom” (or “finished”)?

Will this NetworkStream not respect cancellation?

I can get around this with a combination of Task.Delay()and Task.WhenAny, but I would rather make it work as expected.

Conversely, the following cancellation example:

async void Go(CancellationToken ct)
{
    using(var cts=new CancellationTokenSource(TimeSpan.FromSeconds(5)))
    {
        try
        {
            await Task.Delay(TimeSpan.FromSeconds(10),cts.Token)
                                        .ConfigureAwait(false);
        }
        catch(TaskCanceledException)
        {
            Console.WriteLine("boom");
        }
    }
}

Prints a boom, as expected. What's happening?

+4
source share
1

, NetworkStream .

, API Win32 . - , - . .NET BCL XP API ( ), CancelIoEx.

Stream "" ( - ), . "" , , . , , NetworkStream.

( Win32) , , . ( , ). BCL, , .

cts.Token.Register(() => client.Close());
...
catch (ObjectDisposedException)

, , ( , ), . .

+13

All Articles