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;
}
}
}
Per the description in Softlion's answer:
I've made some code that gives you the async read with timeout:
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:
There are a few problems there that pop out:
CancellationToken
throwsOperationCanceledException
, notTimeoutException
(cancellation is not always due to timeout).ReceiveTimeout
doesn't apply, since you're doing an asynchronous read. Even if it did, you'd have a race condition betweenIOException
andOperationCanceledException
.The correct way to test asynchronous code is with an asynchronous test:
Cancellation is cooperative.
NetworkStream.ReadAsync
must cooperate to be able to be cancelled. It is kind of hard for it to do that because that would potentially leave the stream in an undefined state. What bytes have already been read from the Windows TCP stack and what haven't? IO is not easily cancellable.Reflector shows that
NetworkStream
does not overrideReadAsync
. This means that it will get the default behavior ofStream.ReadAsync
which just throws the token away. There is no generic way Stream operations can be cancelled so the BCLStream
class does not even try (it cannot try - there is no way to do this).You should set a timeout on the
Socket
.I know it's a bit late, but this is a simple thing I usually do to cancel
ReadAsync()
(in my case: NetworkStream) (Tested):}
Edit: I have put that in another
Task
, to clarify.