abstract_tcp_server2: fix race on shutdown

This commit is contained in:
moneromooo-monero 2018-07-11 22:02:28 +01:00
parent eed4dba880
commit 979105b298
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
2 changed files with 9 additions and 1 deletions

View File

@ -158,6 +158,7 @@ namespace net_utils
std::list<boost::shared_ptr<connection<t_protocol_handler> > > m_self_refs; // add_ref/release support std::list<boost::shared_ptr<connection<t_protocol_handler> > > m_self_refs; // add_ref/release support
critical_section m_self_refs_lock; critical_section m_self_refs_lock;
critical_section m_chunking_lock; // held while we add small chunks of the big do_send() to small do_send_chunk() critical_section m_chunking_lock; // held while we add small chunks of the big do_send() to small do_send_chunk()
critical_section m_shutdown_lock; // held while shutting down
t_connection_type m_connection_type; t_connection_type m_connection_type;

View File

@ -649,6 +649,10 @@ PRAGMA_WARNING_DISABLE_VS(4355)
template<class t_protocol_handler> template<class t_protocol_handler>
bool connection<t_protocol_handler>::shutdown() bool connection<t_protocol_handler>::shutdown()
{ {
CRITICAL_REGION_BEGIN(m_shutdown_lock);
if (m_was_shutdown)
return true;
m_was_shutdown = true;
// Initiate graceful connection closure. // Initiate graceful connection closure.
m_timer.cancel(); m_timer.cancel();
boost::system::error_code ignored_ec; boost::system::error_code ignored_ec;
@ -658,7 +662,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
try { host_count(m_host, -1); } catch (...) { /* ignore */ } try { host_count(m_host, -1); } catch (...) { /* ignore */ }
m_host = ""; m_host = "";
} }
m_was_shutdown = true; CRITICAL_REGION_END();
m_protocol_handler.release_protocol(); m_protocol_handler.release_protocol();
return true; return true;
} }
@ -667,6 +671,9 @@ PRAGMA_WARNING_DISABLE_VS(4355)
bool connection<t_protocol_handler>::close() bool connection<t_protocol_handler>::close()
{ {
TRY_ENTRY(); TRY_ENTRY();
auto self = safe_shared_from_this();
if(!self)
return false;
//_info("[sock " << socket_.native_handle() << "] Que Shutdown called."); //_info("[sock " << socket_.native_handle() << "] Que Shutdown called.");
m_timer.cancel(); m_timer.cancel();
size_t send_que_size = 0; size_t send_que_size = 0;