From 46f3a9c03d520c091db319949776161974fbc278 Mon Sep 17 00:00:00 2001 From: Enrico Giordani Date: Fri, 13 Nov 2015 15:03:24 -0400 Subject: [PATCH] [Fix] Duplicated sockets management for diskless replication. --- src/Win32_Interop/Win32_QFork.cpp | 18 ++++++++++-------- src/Win32_Interop/Win32_QFork_impl.c | 10 ++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Win32_Interop/Win32_QFork.cpp b/src/Win32_Interop/Win32_QFork.cpp index a6fecc0d..23ccc367 100644 --- a/src/Win32_Interop/Win32_QFork.cpp +++ b/src/Win32_Interop/Win32_QFork.cpp @@ -354,16 +354,17 @@ BOOL QForkChildInit(HANDLE QForkControlMemoryMapHandle, DWORD ParentProcessID) { } else if (g_pQForkControl->typeOfOperation == OperationType::otSocket) { LPWSAPROTOCOL_INFO lpProtocolInfo = (LPWSAPROTOCOL_INFO) g_pQForkControl->globalData.protocolInfo; int pipe_write_fd = FDAPI_open_osfhandle((intptr_t) g_pQForkControl->globalData.pipe_write_handle, _O_APPEND); + int* fds = (int*) malloc(sizeof(int) * g_pQForkControl->globalData.numfds); for (int i = 0; i < g_pQForkControl->globalData.numfds; i++) { - g_pQForkControl->globalData.fds[i] = FDAPI_WSASocket(FROM_PROTOCOL_INFO, - FROM_PROTOCOL_INFO, - FROM_PROTOCOL_INFO, - &lpProtocolInfo[i], - 0, - WSA_FLAG_OVERLAPPED); + fds[i] = FDAPI_WSASocket(FROM_PROTOCOL_INFO, + FROM_PROTOCOL_INFO, + FROM_PROTOCOL_INFO, + &lpProtocolInfo[i], + 0, + WSA_FLAG_OVERLAPPED); } - g_ChildExitCode = do_socketSave(g_pQForkControl->globalData.fds, + g_ChildExitCode = do_socketSave(fds, g_pQForkControl->globalData.numfds, g_pQForkControl->globalData.clientids, pipe_write_fd); @@ -371,8 +372,9 @@ BOOL QForkChildInit(HANDLE QForkControlMemoryMapHandle, DWORD ParentProcessID) { // Failing to close the sockets properly will produce a socket read error // on both the parent process and the slave. for (int i = 0; i < g_pQForkControl->globalData.numfds; i++) { - FDAPI_CloseDuplicatedSocket(g_pQForkControl->globalData.fds[i]); + FDAPI_CloseDuplicatedSocket(fds[i]); } + free(fds); } else { throw runtime_error("unexpected operation type"); } diff --git a/src/Win32_Interop/Win32_QFork_impl.c b/src/Win32_Interop/Win32_QFork_impl.c index 0d82e4d8..8ed83484 100644 --- a/src/Win32_Interop/Win32_QFork_impl.c +++ b/src/Win32_Interop/Win32_QFork_impl.c @@ -21,6 +21,7 @@ */ #include "..\redis.h" +#include "Win32_Portability.h" void SetupRedisGlobals(LPVOID redisData, size_t redisDataSize, uint32_t dictHashSeed) { @@ -74,12 +75,13 @@ int do_rdbSaveToSlavesSockets(int *fds, int numfds, uint64_t *clientids) server.rdb_child_pid = GetCurrentProcessId(); rioInitWithFdset(&slave_sockets,fds,numfds); - zfree(fds); + // On Windows we need to use the fds after do_socketSave2 has finished + // so we don't free them here, moreover since we allocate the fds in + // QFork.cpp it's better to use malloc instead of zmalloc. + POSIX_ONLY(zfree(fds);); // On Windows we haven't duplicated the listening sockets so we shouldn't close them -#ifndef _WIN32 - closeListeningSockets(0); -#endif + POSIX_ONLY(closeListeningSockets(0);) redisSetProcTitle("redis-rdb-to-slaves"); -- GitLab