Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,10 @@ public Socket Accept()

Debug.Assert(!acceptedSocketHandle.IsInvalid);

Socket socket = CreateAcceptSocket(acceptedSocketHandle, _rightEndPoint.Create(socketAddress));
if (NetEventSource.Log.IsEnabled()) NetEventSource.Accepted(socket, socket.RemoteEndPoint!, socket.LocalEndPoint);
// macOS can return accept() success with an empty remote sockaddr when the peer reset before accept.
EndPoint? remoteEndPoint = socketAddress.Size > 0 ? _rightEndPoint.Create(socketAddress) : null;
Socket socket = CreateAcceptSocket(acceptedSocketHandle, remoteEndPoint);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Accepted(socket, remoteEndPoint, socket.LocalEndPoint);
return socket;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,17 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags

if (socketError == SocketError.Success)
{
_acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket!, _currentSocket._rightEndPoint!.Create(remoteSocketAddress));
// macOS can return accept() success with an empty remote sockaddr when the peer reset before accept.
EndPoint? remoteEndPoint = remoteSocketAddress.Size > 0
? _currentSocket._rightEndPoint!.Create(remoteSocketAddress)
: null;
_acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket!, remoteEndPoint);

if (NetEventSource.Log.IsEnabled())
{
try
{
NetEventSource.Accepted(_acceptSocket, _acceptSocket.RemoteEndPoint, _acceptSocket.LocalEndPoint);
NetEventSource.Accepted(_acceptSocket, remoteEndPoint, _acceptSocket.LocalEndPoint);
}
catch (ObjectDisposedException) { }
}
Expand Down
51 changes: 51 additions & 0 deletions src/libraries/System.Net.Sockets/tests/FunctionalTests/Accept.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,55 @@ public sealed class AcceptEap : Accept<SocketHelperEap>
{
public AcceptEap(ITestOutputHelper output) : base(output) {}
}

public sealed class AcceptDualStackResetTests
{
[ConditionalTheory(typeof(Socket), nameof(Socket.OSSupportsIPv6))]
[SkipOnPlatform(TestPlatforms.Wasi | TestPlatforms.OpenBSD, "These platforms don't support dual-mode sockets")]
[InlineData(false)]
[InlineData(true)]
public async Task Accept_DualStackListener_PeerImmediatelyResets_ListenerStaysHealthy(bool useAsync)
{
for (int i = 0; i < 200; i++)
{
using Socket listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
listener.DualMode = true;
listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
int port = ((IPEndPoint)listener.LocalEndPoint!).Port;
listener.Listen(2);

using Socket ipv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
using Socket ipv4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };

await ipv4.ConnectAsync(IPAddress.Loopback, port).WaitAsync(TimeSpan.FromSeconds(5));
ipv4.LingerState = new LingerOption(true, 0);
ipv4.Close();

await ipv6.ConnectAsync(IPAddress.IPv6Loopback, port).WaitAsync(TimeSpan.FromSeconds(5));
byte[] message = [42];
Assert.Equal(message.Length, ipv6.Send(message));

bool receivedMessage = false;
for (int acceptCount = 0; acceptCount < 2 && !receivedMessage; acceptCount++)
{
using Socket accepted = useAsync
? await listener.AcceptAsync().WaitAsync(TimeSpan.FromSeconds(5))
: listener.Accept();

try
{
byte[] received = new byte[message.Length];
int receivedCount = await accepted.ReceiveAsync(received).WaitAsync(TimeSpan.FromSeconds(5));
receivedMessage = receivedCount == message.Length && received.AsSpan().SequenceEqual(message);
}
catch (SocketException)
{
// Some platforms surface the reset connection from accept(), while others discard it.
}
}

Assert.True(receivedMessage);
}
}
}
}
Loading