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)
commit110b6f1617f506d95a1c8bb3abe1de03a1b36e57
tree5375e7c6e7a284642d3c2c1e42eeac01d5e7f012
parentd378fc8ede4e5ba95ec1e129ff9e1f1c74e3d187
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.
mono/metadata/w32socket.c