@@ -425,6 +425,7 @@ void CConnman::DumpBanlist()
425425void CNode::CloseSocketDisconnect ()
426426{
427427 fDisconnect = true ;
428+ LOCK (cs_hSocket);
428429 if (hSocket != INVALID_SOCKET)
429430 {
430431 LogPrint (" net" , " disconnecting peer=%d\n " , id);
@@ -789,7 +790,13 @@ size_t CConnman::SocketSendData(CNode *pnode) const
789790 while (it != pnode->vSendMsg .end ()) {
790791 const auto &data = *it;
791792 assert (data.size () > pnode->nSendOffset );
792- int nBytes = send (pnode->hSocket , reinterpret_cast <const char *>(data.data ()) + pnode->nSendOffset , data.size () - pnode->nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
793+ int nBytes = 0 ;
794+ {
795+ LOCK (pnode->cs_hSocket );
796+ if (pnode->hSocket == INVALID_SOCKET)
797+ break ;
798+ nBytes = send (pnode->hSocket , reinterpret_cast <const char *>(data.data ()) + pnode->nSendOffset , data.size () - pnode->nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
799+ }
793800 if (nBytes > 0 ) {
794801 pnode->nLastSend = GetSystemTimeInSeconds ();
795802 pnode->nSendBytes += nBytes;
@@ -1150,9 +1157,6 @@ void CConnman::ThreadSocketHandler()
11501157 LOCK (cs_vNodes);
11511158 BOOST_FOREACH (CNode* pnode, vNodes)
11521159 {
1153- if (pnode->hSocket == INVALID_SOCKET)
1154- continue ;
1155-
11561160 // Implement the following logic:
11571161 // * If there is data to send, select() for sending data. As this only
11581162 // happens when optimistic write failed, we choose to first drain the
@@ -1171,6 +1175,10 @@ void CConnman::ThreadSocketHandler()
11711175 select_send = !pnode->vSendMsg .empty ();
11721176 }
11731177
1178+ LOCK (pnode->cs_hSocket );
1179+ if (pnode->hSocket == INVALID_SOCKET)
1180+ continue ;
1181+
11741182 FD_SET (pnode->hSocket , &fdsetError);
11751183 hSocketMax = std::max (hSocketMax, pnode->hSocket );
11761184 have_fds = true ;
@@ -1237,18 +1245,27 @@ void CConnman::ThreadSocketHandler()
12371245 bool recvSet = false ;
12381246 bool sendSet = false ;
12391247 bool errorSet = false ;
1240- if (pnode->hSocket == INVALID_SOCKET)
1241- continue ;
1242- recvSet = FD_ISSET (pnode->hSocket , &fdsetRecv);
1243- sendSet = FD_ISSET (pnode->hSocket , &fdsetSend);
1244- errorSet = FD_ISSET (pnode->hSocket , &fdsetError);
1248+ {
1249+ LOCK (pnode->cs_hSocket );
1250+ if (pnode->hSocket == INVALID_SOCKET)
1251+ continue ;
1252+ recvSet = FD_ISSET (pnode->hSocket , &fdsetRecv);
1253+ sendSet = FD_ISSET (pnode->hSocket , &fdsetSend);
1254+ errorSet = FD_ISSET (pnode->hSocket , &fdsetError);
1255+ }
12451256 if (recvSet || errorSet)
12461257 {
12471258 {
12481259 {
12491260 // typical socket buffer is 8K-64K
12501261 char pchBuf[0x10000 ];
1251- int nBytes = recv (pnode->hSocket , pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1262+ int nBytes = 0 ;
1263+ {
1264+ LOCK (pnode->cs_hSocket );
1265+ if (pnode->hSocket == INVALID_SOCKET)
1266+ continue ;
1267+ nBytes = recv (pnode->hSocket , pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1268+ }
12521269 if (nBytes > 0 )
12531270 {
12541271 bool notify = false ;
@@ -2286,8 +2303,7 @@ void CConnman::Stop()
22862303
22872304 // Close sockets
22882305 BOOST_FOREACH (CNode* pnode, vNodes)
2289- if (pnode->hSocket != INVALID_SOCKET)
2290- CloseSocket (pnode->hSocket );
2306+ pnode->CloseSocketDisconnect ();
22912307 BOOST_FOREACH (ListenSocket& hListenSocket, vhListenSocket)
22922308 if (hListenSocket.socket != INVALID_SOCKET)
22932309 if (!CloseSocket (hListenSocket.socket ))
@@ -2688,9 +2704,6 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
26882704 size_t nBytesSent = 0 ;
26892705 {
26902706 LOCK (pnode->cs_vSend );
2691- if (pnode->hSocket == INVALID_SOCKET) {
2692- return ;
2693- }
26942707 bool optimisticSend (pnode->vSendMsg .empty ());
26952708
26962709 // log total amount of bytes per command
0 commit comments