@@ -19,7 +19,6 @@ Sv2Connman::~Sv2Connman()
19
19
for (const auto & client : m_sv2_clients) {
20
20
LogTrace (BCLog::SV2, " Disconnecting client id=%zu\n " ,
21
21
client.first );
22
- CloseConnection (client.second ->m_id );
23
22
client.second ->m_disconnect_flag = true ;
24
23
}
25
24
DisconnectFlagged ();
@@ -64,7 +63,8 @@ void Sv2Connman::DisconnectFlagged()
64
63
// Remove clients that are flagged for disconnection.
65
64
auto it = m_sv2_clients.begin ();
66
65
while (it != m_sv2_clients.end ()) {
67
- if (it->second ->m_disconnect_flag ) {
66
+ if (it->second ->m_send_messages .empty () && it->second ->m_disconnect_flag ) {
67
+ CloseConnection (it->second ->m_id );
68
68
it = m_sv2_clients.erase (it);
69
69
} else {
70
70
it++;
@@ -203,7 +203,6 @@ void Sv2Connman::EventGotData(Id id, std::span<const uint8_t> data)
203
203
// Serious transport problem
204
204
LogPrintLevel (BCLog::SV2, BCLog::Level::Trace, " Transport problem, disconnecting client id=%zu\n " ,
205
205
client->m_id );
206
- CloseConnection (id);
207
206
// TODO: should we even bother with this?
208
207
client->m_disconnect_flag = true ;
209
208
break ;
@@ -217,7 +216,6 @@ void Sv2Connman::EventGotData(Id id, std::span<const uint8_t> data)
217
216
}
218
217
} catch (const std::exception& e) {
219
218
LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Received error when processing client id=%zu message: %s\n " , client->m_id , e.what ());
220
- CloseConnection (id);
221
219
client->m_disconnect_flag = true ;
222
220
}
223
221
@@ -227,15 +225,13 @@ void Sv2Connman::EventGotEOF(NodeId node_id)
227
225
{
228
226
auto client{WITH_LOCK (m_clients_mutex, return GetClientById (node_id);)};
229
227
if (client == nullptr ) return ;
230
- CloseConnection (node_id);
231
228
client->m_disconnect_flag = true ;
232
229
}
233
230
234
231
void Sv2Connman::EventGotPermanentReadError (NodeId node_id, const std::string& errmsg)
235
232
{
236
233
auto client{WITH_LOCK (m_clients_mutex, return GetClientById (node_id);)};
237
234
if (client == nullptr ) return ;
238
- CloseConnection (node_id);
239
235
client->m_disconnect_flag = true ;
240
236
}
241
237
@@ -250,6 +246,13 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
250
246
251
247
DataStream ss (sv2_net_msg.m_msg );
252
248
249
+ if (client.m_disconnect_flag ) {
250
+ // Don't bother processing new messages if we are about to disconnect when the
251
+ // send queue empties. This also prevents us from appending to the send queue
252
+ // when m_disconnect_flag is set.
253
+ return ;
254
+ }
255
+
253
256
switch (sv2_net_msg.m_msg_type )
254
257
{
255
258
case Sv2MsgType::SETUP_CONNECTION:
@@ -266,7 +269,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
266
269
} catch (const std::exception& e) {
267
270
LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Received invalid SetupConnection message from client id=%zu: %s\n " ,
268
271
client.m_id , e.what ());
269
- CloseConnection (client.m_id );
270
272
client.m_disconnect_flag = true ;
271
273
return ;
272
274
}
@@ -279,7 +281,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
279
281
client.m_id );
280
282
client.m_send_messages .emplace_back (setup_conn_err);
281
283
282
- CloseConnection (client.m_id );
283
284
client.m_disconnect_flag = true ;
284
285
return ;
285
286
}
@@ -293,7 +294,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
293
294
294
295
LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Received a connection from client id=%zu with incompatible protocol_versions: min_version: %d, max_version: %d\n " ,
295
296
client.m_id , setup_conn.m_min_version , setup_conn.m_max_version );
296
- CloseConnection (client.m_id );
297
297
client.m_disconnect_flag = true ;
298
298
return ;
299
299
}
@@ -310,7 +310,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
310
310
case Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS:
311
311
{
312
312
if (!client.m_setup_connection_confirmed ) {
313
- CloseConnection (client.m_id );
314
313
client.m_disconnect_flag = true ;
315
314
return ;
316
315
}
@@ -322,7 +321,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
322
321
} catch (const std::exception& e) {
323
322
LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Received invalid CoinbaseOutputConstraints message from client id=%zu: %s\n " ,
324
323
client.m_id , e.what ());
325
- CloseConnection (client.m_id );
326
324
client.m_disconnect_flag = true ;
327
325
return ;
328
326
}
@@ -333,7 +331,6 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie
333
331
if (max_additional_size > MAX_BLOCK_WEIGHT) {
334
332
LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Received impossible CoinbaseOutputConstraints from client id=%zu: %d\n " ,
335
333
client.m_id , max_additional_size);
336
- CloseConnection (client.m_id );
337
334
client.m_disconnect_flag = true ;
338
335
return ;
339
336
}
0 commit comments