Description
Socket.ConnetAsync sporadically returns successfully but the socket is not connected (Socket.Connected == false). This occurs when canceling ConnectAsync while it's in progress after the server side accepts the socket.
Reproduction Steps
using System.Net;
using System.Net.Sockets;
while (true)
{
using var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
serverSocket.Listen();
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
using var cts = new CancellationTokenSource();
ValueTask connectTask = socket.ConnectAsync(serverSocket.LocalEndPoint!, cts.Token);
using var _ = await serverSocket.AcceptAsync();
cts.Cancel();
try
{
await connectTask;
if (!socket.Connected)
{
Console.Error.WriteLine("unexpected non-connected socket after successfull ConnectAsync");
return 1;
}
}
catch (OperationCanceledException)
{
// Expected if canceled before connect.
}
}
Expected behavior
The ConnectAsync call should either throw OperationCanceledException or return a connected socket.
Actual behavior
ConnectAsync returns but the socket is not connected
Regression?
No response
Known Workarounds
No response
Configuration
.NET Core 6
Linux Debian Buster and macOS
Other information
No response
Description
Socket.ConnetAsyncsporadically returns successfully but the socket is not connected (Socket.Connected == false). This occurs when cancelingConnectAsyncwhile it's in progress after the server side accepts the socket.Reproduction Steps
Expected behavior
The
ConnectAsynccall should either throwOperationCanceledExceptionor return a connected socket.Actual behavior
ConnectAsyncreturns but the socket is not connectedRegression?
No response
Known Workarounds
No response
Configuration
.NET Core 6
Linux Debian Buster and macOS
Other information
No response