Fix SocketResponder on Windows
authorNiklas Therning <niklas@therning.org>
Mon, 10 Oct 2016 14:04:49 +0000 (16:04 +0200)
committerNiklas Therning <niklas@therning.org>
Mon, 10 Oct 2016 15:06:43 +0000 (17:06 +0200)
There seems to be a race in some tests in the System test suite that use
SocketResponder on Windows. Some tests fail with an exception that the socket
has been closed before they get a chance to see the response sent by the
SocketResponder. The same happens occasionally when using SocketResponder on
.NET. This patch changes the order of the Socket.Shutdown() calls and adds a
Socket.Receive() before Shutdown() is called to give the sender a chance to
actually send any data before the responder shuts down and closes its side of
the connection.

mcs/class/test-helpers/SocketResponder.cs

index 118d89410e2efbb38ce56371da9b5012593e7e5a..74e77b0edf0f66df063ca925485701b4a139b671 100644 (file)
@@ -91,8 +91,11 @@ namespace MonoTests.Helpers
                                        listenSocket = tcpListener.AcceptSocket ();
                                        listenSocket.Send (requestHandler (listenSocket));
                                        try {
-                                               listenSocket.Shutdown (SocketShutdown.Receive);
+                                               // On Windows a Receive() is needed here before Shutdown() to consume the data some tests send.
+                                               listenSocket.ReceiveTimeout = 10 * 1000;
+                                               listenSocket.Receive (new byte [0]);
                                                listenSocket.Shutdown (SocketShutdown.Send);
+                                               listenSocket.Shutdown (SocketShutdown.Receive);
                                        } catch {
                                        }
                                } catch (SocketException ex) {