Search code examples
.netsockets.net-4.5async-awaitcancellation-token

NetworkStream.ReadAsync with a cancellation token never cancels


Here the proof.
Any idea what is wrong in this code ?

    [TestMethod]
    public void TestTest()
    {
        var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 };
        tcp.Connect(IPAddress.Parse("176.31.100.115"), 25);
        bool ok = Read(tcp.GetStream()).Wait(30000);
        Assert.IsTrue(ok);
    }

    async Task Read(NetworkStream stream)
    {
        using (var cancellationTokenSource = new CancellationTokenSource(5000))
        {
            int receivedCount;
            try
            {
                var buffer = new byte[1000];
                receivedCount = await stream.ReadAsync(buffer, 0, 1000, cancellationTokenSource.Token);
            }
            catch (TimeoutException e)
            {
                receivedCount = -1;
            }
        }
    }

Solution

  • I finally found a workaround. Combine the async call with a delay task (Task.Delay) using Task.WaitAny. When the delay elapses before the io task, close the stream. This will force the task to stop. You should handle the async exception on the io task correctly. And you should add a continuation task for both the delayed task and the io task.

    It also work with tcp connections. Closing the connection in another thread (you could consider it is the delay task thread) forces all async tasks using/waiting for this connection to stop.

    --EDIT--

    Another cleaner solution suggested by @vtortola: use the cancellation token to register a call to stream.Close:

    async ValueTask Read(NetworkStream stream, TimeSpan timeout = default)
    {
        if(timeout == default(TimeSpan))
          timeout = TimeSpan.FromSeconds(5);
    
        using var cts = new CancellationTokenSource(timeout); //C# 8 syntax
        using(cts.Token.Register(() => stream.Close()))
        {
           int receivedCount;
           try
           {
               var buffer = new byte[30000];
               receivedCount = await stream.ReadAsync(buffer, 0, 30000, tcs.Token).ConfigureAwait(false);
           }
           catch (TimeoutException)
           {
               receivedCount = -1;
           }
        }
    }