@@ -455,6 +455,7 @@ void CConnman::DumpBanlist()
455455void CNode::CloseSocketDisconnect ()
456456{
457457 fDisconnect = true ;
458+ LOCK (cs_hSocket);
458459 if (hSocket != INVALID_SOCKET) {
459460 LogPrint (BCLog::NET, " disconnecting peer=%d\n " , id);
460461 CloseSocket (hSocket);
@@ -820,7 +821,13 @@ size_t CConnman::SocketSendData(CNode* pnode)
820821 while (it != pnode->vSendMsg .end ()) {
821822 const auto & data = *it;
822823 assert (data.size () > pnode->nSendOffset );
823- int nBytes = send (pnode->hSocket , reinterpret_cast <const char *>(data.data ()) + pnode->nSendOffset , data.size () - pnode->nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
824+ int nBytes = 0 ;
825+ {
826+ LOCK (pnode->cs_hSocket );
827+ if (pnode->hSocket == INVALID_SOCKET)
828+ break ;
829+ nBytes = send (pnode->hSocket , reinterpret_cast <const char *>(data.data ()) + pnode->nSendOffset , data.size () - pnode->nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
830+ }
824831 if (nBytes > 0 ) {
825832 pnode->nLastSend = GetTime ();
826833 pnode->nSendBytes += nBytes;
@@ -1154,9 +1161,6 @@ void CConnman::ThreadSocketHandler()
11541161 {
11551162 LOCK (cs_vNodes);
11561163 for (CNode* pnode : vNodes) {
1157- if (pnode->hSocket == INVALID_SOCKET)
1158- continue ;
1159-
11601164 // Implement the following logic:
11611165 // * If there is data to send, select() for sending data. As this only
11621166 // happens when optimistic write failed, we choose to first drain the
@@ -1175,6 +1179,10 @@ void CConnman::ThreadSocketHandler()
11751179 select_send = !pnode->vSendMsg .empty ();
11761180 }
11771181
1182+ LOCK (pnode->cs_hSocket );
1183+ if (pnode->hSocket == INVALID_SOCKET)
1184+ continue ;
1185+
11781186 FD_SET (pnode->hSocket , &fdsetError);
11791187 hSocketMax = std::max (hSocketMax, pnode->hSocket );
11801188 have_fds = true ;
@@ -1236,17 +1244,26 @@ void CConnman::ThreadSocketHandler()
12361244 bool recvSet = false ;
12371245 bool sendSet = false ;
12381246 bool errorSet = false ;
1239- if (pnode->hSocket == INVALID_SOCKET)
1240- continue ;
1241- recvSet = FD_ISSET (pnode->hSocket , &fdsetRecv);
1242- sendSet = FD_ISSET (pnode->hSocket , &fdsetSend);
1243- errorSet = FD_ISSET (pnode->hSocket , &fdsetError);
1247+ {
1248+ LOCK (pnode->cs_hSocket );
1249+ if (pnode->hSocket == INVALID_SOCKET)
1250+ continue ;
1251+ recvSet = FD_ISSET (pnode->hSocket , &fdsetRecv);
1252+ sendSet = FD_ISSET (pnode->hSocket , &fdsetSend);
1253+ errorSet = FD_ISSET (pnode->hSocket , &fdsetError);
1254+ }
12441255 if (recvSet || errorSet) {
12451256 {
12461257 {
12471258 // typical socket buffer is 8K-64K
12481259 char pchBuf[0x10000 ];
1249- int nBytes = recv (pnode->hSocket , pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1260+ int nBytes = 0 ;
1261+ {
1262+ LOCK (pnode->cs_hSocket );
1263+ if (pnode->hSocket == INVALID_SOCKET)
1264+ continue ;
1265+ nBytes = recv (pnode->hSocket , pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1266+ }
12501267 if (nBytes > 0 ) {
12511268 bool notify = false ;
12521269 if (!pnode->ReceiveMsgBytes (pchBuf, nBytes, notify))
@@ -2180,8 +2197,7 @@ void CConnman::Stop()
21802197
21812198 // Close sockets
21822199 for (CNode* pnode : vNodes)
2183- if (pnode->hSocket != INVALID_SOCKET)
2184- CloseSocket (pnode->hSocket );
2200+ pnode->CloseSocketDisconnect ();
21852201 for (ListenSocket& hListenSocket : vhListenSocket)
21862202 if (hListenSocket.socket != INVALID_SOCKET)
21872203 if (!CloseSocket (hListenSocket.socket ))
@@ -2512,9 +2528,6 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
25122528 size_t nBytesSent = 0 ;
25132529 {
25142530 LOCK (pnode->cs_vSend );
2515- if (pnode->hSocket == INVALID_SOCKET) {
2516- return ;
2517- }
25182531 bool optimisticSend (pnode->vSendMsg .empty ());
25192532
25202533 // log total amount of bytes per command
0 commit comments