From 1ce15535f050bff064abff01261b64c8f721c0d8 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Mon, 10 Sep 2012 23:40:27 +0000 Subject: [PATCH 1/2] Bugfix: Don't consider invalid listening socket in hSocketMax Fixed upstream in 8f10a2889089af1b2ac64802360494b54c8c7ff1. --- src/net.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/net.cpp b/src/net.cpp index d2fa111d233..b479ff141e7 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -843,8 +843,10 @@ void ThreadSocketHandler2(void* parg) SOCKET hSocketMax = 0; if(hListenSocket != INVALID_SOCKET) + { FD_SET(hListenSocket, &fdsetRecv); - hSocketMax = max(hSocketMax, hListenSocket); + hSocketMax = max(hSocketMax, hListenSocket); + } CRITICAL_BLOCK(cs_vNodes) { BOOST_FOREACH(CNode* pnode, vNodes) From 4ad495c2433a16edc1b6e2d2426fcefa69d06cd7 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 5 Sep 2012 16:01:28 -0400 Subject: [PATCH 2/2] select(): Use precise fd presence check, rather than imprecise hSocketMax test --- src/net.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index b479ff141e7..e526da0ac5c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -841,11 +841,13 @@ void ThreadSocketHandler2(void* parg) FD_ZERO(&fdsetSend); FD_ZERO(&fdsetError); SOCKET hSocketMax = 0; + bool have_fds = false; if(hListenSocket != INVALID_SOCKET) { FD_SET(hListenSocket, &fdsetRecv); hSocketMax = max(hSocketMax, hListenSocket); + have_fds = true; } CRITICAL_BLOCK(cs_vNodes) { @@ -856,6 +858,7 @@ void ThreadSocketHandler2(void* parg) FD_SET(pnode->hSocket, &fdsetRecv); FD_SET(pnode->hSocket, &fdsetError); hSocketMax = max(hSocketMax, pnode->hSocket); + have_fds = true; TRY_CRITICAL_BLOCK(pnode->cs_vSend) if (!pnode->vSend.empty()) FD_SET(pnode->hSocket, &fdsetSend); @@ -863,15 +866,16 @@ void ThreadSocketHandler2(void* parg) } vnThreadsRunning[0]--; - int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout); + int nSelect = select(have_fds ? hSocketMax + 1 : 0, + &fdsetRecv, &fdsetSend, &fdsetError, &timeout); vnThreadsRunning[0]++; if (fShutdown) return; if (nSelect == SOCKET_ERROR) { - int nErr = WSAGetLastError(); - if (hSocketMax != INVALID_SOCKET) + if (have_fds) { + int nErr = WSAGetLastError(); printf("socket select error %d\n", nErr); for (unsigned int i = 0; i <= hSocketMax; i++) FD_SET(i, &fdsetRecv);