From: lateralusX Date: Thu, 14 Sep 2017 09:20:27 +0000 (+0200) Subject: Invalid handle exception during WSACleanup on shutdown. X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mono.git;a=commitdiff_plain;h=110b6f1617f506d95a1c8bb3abe1de03a1b36e57 Invalid handle exception during WSACleanup on shutdown. 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. --- diff --git a/mono/metadata/w32socket.c b/mono/metadata/w32socket.c index 43df41bfdc7..bf789f1d00d 100644 --- a/mono/metadata/w32socket.c +++ b/mono/metadata/w32socket.c @@ -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; }