Invalid handle exception during WSACleanup on shutdown.
authorlateralusX <lateralusx.github@gmail.com>
Thu, 14 Sep 2017 09:20:27 +0000 (11:20 +0200)
committerlateralusX <lateralusx.github@gmail.com>
Thu, 14 Sep 2017 09:20:27 +0000 (11:20 +0200)
Running the MonoTests.System.Net.HttpRequestStreamTest under debugger reveals this
problem. WSACleanup is freeing an invalid handle as part of network shutdown.

This happens since mono_w32socket_close calls incorrect win32 API when closing sockets.
It currently calls CloseHandle, while it should call closesocket. This probably cause
problems in winsock book keeping of active sockets, and when closing the network layer,
winsock will try to close handles that it still open, but since the handles have already been closed
by incorrect call to CloseHandle, it will close an “invalid handle” either causing the exception
or even worse, close a different handle currently in use, causing undefined behavior
during the rest of mono shutdown.

Fix is to switch to correct API when closing sockets on Windows, closesocket instread of CloseHandle.

mono/metadata/w32socket.c

index 43df41bfdc717181022977d5bbc39853e43bdb87..bf789f1d00da93972c6b897f42d51c2f75a73071 100644 (file)
@@ -208,7 +208,7 @@ mono_w32socket_close (SOCKET sock)
 {
        gboolean ret;
        MONO_ENTER_GC_SAFE;
-       ret = CloseHandle (sock);
+       ret = closesocket (sock);
        MONO_EXIT_GC_SAFE;
        return ret;
 }