From c06f3c4760d49d69b28abca0d7530b47e627c021 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 6 Jan 2021 13:22:48 +0100 Subject: [PATCH] Fix Socket.ReceiveFrom on Unix (#46151) Fix a regression within the sync-over-async blocking path within the Unix ReceiveFrom implementation. --- .../src/System/Net/Sockets/SocketAsyncContext.Unix.cs | 1 + .../tests/FunctionalTests/SendReceive/SendReceive.cs | 1 + .../tests/FunctionalTests/SendReceive/SendReceiveNonParallel.cs | 4 ++++ .../System.Net.Sockets/tests/FunctionalTests/SocketTestHelper.cs | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs index 343c9c9..4202cdb 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @@ -1505,6 +1505,7 @@ namespace System.Net.Sockets { Buffer = buffer, Flags = flags, + SetReceivedFlags = true, SocketAddress = socketAddress, SocketAddressLen = socketAddressLen, }; diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceive.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceive.cs index dfef96e..a96680a 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceive.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceive.cs @@ -873,6 +873,7 @@ namespace System.Net.Sockets.Tests var socket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); if (address.IsIPv4MappedToIPv6) socket.DualMode = true; socket.BindToAnonymousPort(address); + ConfigureNonBlocking(socket); Task receiveTask = ReceiveAsync(socket, new ArraySegment(new byte[1])); diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceiveNonParallel.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceiveNonParallel.cs index 02cb9a0..634d0f4 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceiveNonParallel.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive/SendReceiveNonParallel.cs @@ -40,6 +40,10 @@ namespace System.Net.Sockets.Tests using var left = useClone ? new Socket(origLeft.SafeHandle) : origLeft; using var right = useClone ? new Socket(origRight.SafeHandle) : origRight; + // Force non-blocking mode in ...SyncForceNonBlocking variants of the test: + ConfigureNonBlocking(left); + ConfigureNonBlocking(right); + var leftEndpoint = (IPEndPoint)left.LocalEndPoint; var rightEndpoint = (IPEndPoint)right.LocalEndPoint; diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketTestHelper.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketTestHelper.cs index 41fa116..b6ff517 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketTestHelper.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketTestHelper.cs @@ -41,6 +41,7 @@ namespace System.Net.Sockets.Tests public virtual bool SupportsAcceptIntoExistingSocket => true; public virtual bool SupportsAcceptReceive => false; public virtual void Listen(Socket s, int backlog) { s.Listen(backlog); } + public virtual void ConfigureNonBlocking(Socket s) { } } public class SocketHelperArraySync : SocketHelperBase @@ -91,6 +92,7 @@ namespace System.Net.Sockets.Tests s.Listen(backlog); s.ForceNonBlocking(true); } + public override void ConfigureNonBlocking(Socket s) => s.ForceNonBlocking(true); } public sealed class SocketHelperApm : SocketHelperBase @@ -345,6 +347,7 @@ namespace System.Net.Sockets.Tests public bool SupportsAcceptIntoExistingSocket => _socketHelper.SupportsAcceptIntoExistingSocket; public bool SupportsAcceptReceive => _socketHelper.SupportsAcceptReceive; public void Listen(Socket s, int backlog) => _socketHelper.Listen(s, backlog); + public void ConfigureNonBlocking(Socket s) => _socketHelper.ConfigureNonBlocking(s); } public class SocketHelperSpanSync : SocketHelperArraySync @@ -364,6 +367,7 @@ namespace System.Net.Sockets.Tests Task.Run(() => { s.ForceNonBlocking(true); Socket accepted = s.Accept(); accepted.ForceNonBlocking(true); return accepted; }); public override Task ConnectAsync(Socket s, EndPoint endPoint) => Task.Run(() => { s.ForceNonBlocking(true); s.Connect(endPoint); }); + public override void ConfigureNonBlocking(Socket s) => s.ForceNonBlocking(true); } public sealed class SocketHelperMemoryArrayTask : SocketHelperTask -- 2.7.4