-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cancellation on NetworkStream.ReadAsync closes local connection but remote server end-point still seems to be connected #69397
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionUsing .NET 6 on Windows 10 For a multithreaded Behavior remains same for Reproduction StepsFor client/server scenario with
Expected behavior
Actual behavior
Regression?No response Known WorkaroundsNo response Configuration
Other informationNo response
|
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionUsing .NET 6 on Windows 10 For a multithreaded Behavior remains same for Reproduction StepsFor client/server scenario with
Expected behavior
Actual behavior
Regression?No response Known WorkaroundsNo response Configuration
Other informationNo response
|
Do you have simple repo @zahirtezcan-bugs ? I suspect we hit As far as leaving the original state: It is tricky to ensure consistency as the cancelation can be triggered at different places. e.g. some bytes may be read from OS but not delivered to application. Preserving such data and order with multiple reads may be non trivial. |
I was trying this in my company network, sorry that I could not export actual code but I have added a repro and kept things simpler, for example client operates in single loop writes some data and reads in a loop indefinitely. https://github.com/zahirtezcan-bugs/AsyncTcp Note: In all of my tries I always closed the client via |
Triage: we should look into this, but not high-prio. |
Thanks for reporting this problem, wasted an hour trying to figure out why the |
NetworkStream does not support non-blocking I/O (discussion here: #22506) and this issue prevents us using timeout-cancellation. Is there a way to use asynchronous API and perform an non-blocking-wait on a read operation with timeout? |
What is your goal @zahirtezcan-bugs? I think you can make non-blocking work with The |
I put up #99181 for the cancellation. The the EOF received on server should be indicative (when non zero buffer is provided) that client will not send any more data. The |
Just to echo the previous comment, I just ran into the same issue with the Connected flag being set to false when ReadAsync was cancelled via a cancellation token, and until I found this thread it threw me for quite a long debugging detour. This would be great to get fixed, it's incredibly unintuitive. |
The fix for making |
This issue has been marked |
This issue has been automatically marked |
This issue will now be closed since it had been marked |
Description
Using .NET 6 on Windows 10
For a multithreaded
TcpClient
one loop for receiving and other loop for sending messages, if we useNetworkStream.ReadAsync
with aCancellationToken
, and if the token gets canceled1-
ReadAsync
throws a cancellation exception, which is expected (documentation ofReadAsync
does not declare it though)2-
TcpClient.Connected
becomesfalse
, which is not-expected (or at least I did not)3-
TcpClient.Connected
on server side (the client returned fromTcpListener.AcceptClientAsync
) staystrue
, which is not-expected.Behavior remains same for
Reproduction Steps
For client/server scenario with
TcpListener
andTcpClient
classes;127.0.0.1:13337
and buffer-size is24
, also maintain aCancellationToken
in each process to trigger the error. (I am usingMicrosoft.Extensions.Hosting
and triggering cancellation via CTRL+C)TcpListener
viaStart
method and waits for a client withAcceptTcpClientAsync
TcpClient
is received run an asynchronous loop viaTask.Run
NetworkStream.ReadAsync
and echo bytes back viaNetworkStream.WriteAsync
ReadAsync
in another loop to guarantee reading buffer-size bytes before writing back.TcpClient
with empty constructor and enters a loop by callingConnectAsync
Task.Run
(is this OK?)NetworkStream.ReadAsync
andNetworkStream.WriteAsync
accordingly butWriteAsync
works only a limited number of times to makeReadAsync
eventually waitReadAsync
) we trigger cancellationReadAsync
exits via anOperationCanceledException
butTcpClient.Connected
returns tofalse
and further communication is not possible.ReadAsync
calls returns immediately with zero0
read-count butTcpClient.Connected
remainstrue
on the server-side. Even if we callTcpClient.Close
on the client-side, server-side will still reportConnected
astrue
ReadAsync
as expected.Expected behavior
NetworkStream.ReadAsync
should not affect connection status for a cancellation viaCancellationToken
NetworkStream.ReadAsync
needs to close connection then it should do it properly so that listener side can close the client properly.Actual behavior
CancellationToken
gets canceled whileNetworkStream.ReadAsync
is waiting on a message from network, the connection gets closed on the client-sideTcpClient.Connected
still reports connected but itsNetworkStream.ReadAsync
always returns with zero0
count immediately.Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: