@@ -425,6 +425,7 @@ void CConnman::DumpBanlist()
425
425
void CNode::CloseSocketDisconnect ()
426
426
{
427
427
fDisconnect = true ;
428
+ LOCK (cs_hSocket);
428
429
if (hSocket != INVALID_SOCKET)
429
430
{
430
431
LogPrint (" net" , " disconnecting peer=%d\n " , id);
@@ -789,7 +790,13 @@ size_t CConnman::SocketSendData(CNode *pnode) const
789
790
while (it != pnode->vSendMsg .end ()) {
790
791
const auto &data = *it;
791
792
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
+ }
793
800
if (nBytes > 0 ) {
794
801
pnode->nLastSend = GetSystemTimeInSeconds ();
795
802
pnode->nSendBytes += nBytes;
@@ -1150,9 +1157,6 @@ void CConnman::ThreadSocketHandler()
1150
1157
LOCK (cs_vNodes);
1151
1158
BOOST_FOREACH (CNode* pnode, vNodes)
1152
1159
{
1153
- if (pnode->hSocket == INVALID_SOCKET)
1154
- continue ;
1155
-
1156
1160
// Implement the following logic:
1157
1161
// * If there is data to send, select() for sending data. As this only
1158
1162
// happens when optimistic write failed, we choose to first drain the
@@ -1171,6 +1175,10 @@ void CConnman::ThreadSocketHandler()
1171
1175
select_send = !pnode->vSendMsg .empty ();
1172
1176
}
1173
1177
1178
+ LOCK (pnode->cs_hSocket );
1179
+ if (pnode->hSocket == INVALID_SOCKET)
1180
+ continue ;
1181
+
1174
1182
FD_SET (pnode->hSocket , &fdsetError);
1175
1183
hSocketMax = std::max (hSocketMax, pnode->hSocket );
1176
1184
have_fds = true ;
@@ -1237,18 +1245,27 @@ void CConnman::ThreadSocketHandler()
1237
1245
bool recvSet = false ;
1238
1246
bool sendSet = false ;
1239
1247
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
+ }
1245
1256
if (recvSet || errorSet)
1246
1257
{
1247
1258
{
1248
1259
{
1249
1260
// typical socket buffer is 8K-64K
1250
1261
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
+ }
1252
1269
if (nBytes > 0 )
1253
1270
{
1254
1271
bool notify = false ;
@@ -2286,8 +2303,7 @@ void CConnman::Stop()
2286
2303
2287
2304
// Close sockets
2288
2305
BOOST_FOREACH (CNode* pnode, vNodes)
2289
- if (pnode->hSocket != INVALID_SOCKET)
2290
- CloseSocket (pnode->hSocket );
2306
+ pnode->CloseSocketDisconnect ();
2291
2307
BOOST_FOREACH (ListenSocket& hListenSocket, vhListenSocket)
2292
2308
if (hListenSocket.socket != INVALID_SOCKET)
2293
2309
if (!CloseSocket (hListenSocket.socket ))
@@ -2688,9 +2704,6 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2688
2704
size_t nBytesSent = 0 ;
2689
2705
{
2690
2706
LOCK (pnode->cs_vSend );
2691
- if (pnode->hSocket == INVALID_SOCKET) {
2692
- return ;
2693
- }
2694
2707
bool optimisticSend (pnode->vSendMsg .empty ());
2695
2708
2696
2709
// log total amount of bytes per command
0 commit comments