From 37f55d45f3497e70b8875e4e96166d9fd15220ae Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 21 Jan 2020 10:21:55 +0100 Subject: [PATCH] Platform_pipe: call WSAStartup/WSACleanup explicitly Not all pipes are destroyed upon common_cleanup call (which calls WSACleanup). --- src/compat/platform_pipe.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/compat/platform_pipe.cpp b/src/compat/platform_pipe.cpp index 97c0dc811..92beec118 100644 --- a/src/compat/platform_pipe.cpp +++ b/src/compat/platform_pipe.cpp @@ -116,6 +116,35 @@ static void * worker(void *args) int platform_pipe_init(fd_t p[2]) { +#ifdef WIN32 + // This needs to be called twice, indeed, since WSACleanup will be run + // on both ends. This call is also required because not all pipes are + // destroyed before common_cleanup which calls WSACleanup + WSADATA wsaData; + int err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error %d.", err); + return -1; + } + if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { + fprintf(stderr, "Counld not found usable version of Winsock.\n"); + WSACleanup(); + return -1; + } + err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error %d.", err); + WSACleanup(); + return -1; + } + if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { + fprintf(stderr, "Counld not found usable version of Winsock.\n"); + WSACleanup(); + WSACleanup(); + return -1; + } +#endif + struct params par; fd_t sock = open_socket(&par.port); if (sock == INVALID_SOCKET) { @@ -146,5 +175,8 @@ int platform_pipe_init(fd_t p[2]) void platform_pipe_close(fd_t pipe) { CLOSESOCKET(pipe); +#ifdef WIN32 + WSACleanup(); +#endif }