Incorrect socket close causes data truncation on NetFx #13219
Labels
affected-very-few
This issue impacts very few customers
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
bug
This issue describes a behavior which is not expected - a bug.
feature-kestrel
severity-nice-to-have
This label is used by an internal tool
Milestone
Describe the bug
When using a custom connection handler, when closing the connection by calling Complete on the input and output pipes, the method AspNetCore uses to close the socket can lead to some bytes which were "completed" on the output pipe to not be sent before the socket is closed. This only happens on NetFx and doesn't occur on .NET Core.
CoreWCF implements the WCF Net.Tcp framing protocol. As part of this protocol, when closing the connection a final single framing FIN byte is sent. Then the connection is closed. Due to NetFx having a different way how it closes the socket, that final FIN byte might not get sent. It's very timing sensitive as insertion of a small delay before closing results in the final byte being sent.
AspNetCore closes the socket by calling Socket.Shutdown(SocketShutdown.Both), then calls Dispose on the socket. The correct sequence is:
Socket.Shutdown(SocketShutdown.Send);
int bytesRead = Socket.Receive(dummy, 0, 1, SocketFlags.None); // dummy is a buffer of at least 1 byte. Also need a timeout configures
if (bytesRead > 0)
{
Socket.Abort();
// Potentially throw of log
}
else
{
Socket.Close(); // Preferably with a timeout
}
To Reproduce
Steps to reproduce the behavior:
Expected behavior
All bytes sent to be received by remote party
The text was updated successfully, but these errors were encountered: