Merge pull request #7192

95575cda6 epee: fix some issues using connections after shutdown (moneromooo-monero)
This commit is contained in:
Alexander Blair 2020-12-26 13:44:09 -08:00
commit 7438617bbf
No known key found for this signature in database
GPG Key ID: C64552D877C32479

View File

@ -334,6 +334,9 @@ PRAGMA_WARNING_DISABLE_VS(4355)
TRY_ENTRY(); TRY_ENTRY();
//_info("[sock " << socket().native_handle() << "] Async read calledback."); //_info("[sock " << socket().native_handle() << "] Async read calledback.");
if (m_was_shutdown)
return;
if (!e) if (!e)
{ {
double current_speed_down; double current_speed_down;
@ -360,6 +363,9 @@ PRAGMA_WARNING_DISABLE_VS(4355)
CRITICAL_REGION_LOCAL( epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_in ); CRITICAL_REGION_LOCAL( epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_in );
delay = epee::net_utils::network_throttle_manager::get_global_throttle_in().get_sleep_time_after_tick( bytes_transferred ); delay = epee::net_utils::network_throttle_manager::get_global_throttle_in().get_sleep_time_after_tick( bytes_transferred );
} }
if (m_was_shutdown)
return;
delay *= 0.5; delay *= 0.5;
long int ms = (long int)(delay * 100); long int ms = (long int)(delay * 100);
@ -431,6 +437,9 @@ PRAGMA_WARNING_DISABLE_VS(4355)
std::size_t bytes_transferred) std::size_t bytes_transferred)
{ {
TRY_ENTRY(); TRY_ENTRY();
if (m_was_shutdown) return;
if (e) if (e)
{ {
// offload the error case // offload the error case
@ -438,12 +447,15 @@ PRAGMA_WARNING_DISABLE_VS(4355)
return; return;
} }
reset_timer(get_timeout_from_bytes_read(bytes_transferred), false); buffer_ssl_init_fill = bytes_transferred;
MTRACE("we now have " << buffer_ssl_init_fill << "/" << get_ssl_magic_size() << " bytes needed to detect SSL");
buffer_ssl_init_fill += bytes_transferred; if (buffer_ssl_init_fill < get_ssl_magic_size())
if (buffer_ssl_init_fill <= get_ssl_magic_size())
{ {
socket().async_receive(boost::asio::buffer(buffer_.data() + buffer_ssl_init_fill, buffer_.size() - buffer_ssl_init_fill), // don't busy loop on this, ideally we'd want to queue a "async_receive_if_new_data" but there doesn't
// seem to be something like that in boost if we want to just peek at the data, so we'd need to copy and
// have a bit more code just for this. Though I'm just seeing async_read_until which might just work.
epee::misc_utils::sleep_no_w(100);
socket().async_receive(boost::asio::buffer(buffer_.data(), buffer_.size()),
boost::asio::socket_base::message_peek, boost::asio::socket_base::message_peek,
strand_.wrap( strand_.wrap(
boost::bind(&connection<t_protocol_handler>::handle_receive, connection<t_protocol_handler>::shared_from_this(), boost::bind(&connection<t_protocol_handler>::handle_receive, connection<t_protocol_handler>::shared_from_this(),
@ -651,6 +663,8 @@ PRAGMA_WARNING_DISABLE_VS(4355)
boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) ); boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) );
m_send_que_lock.lock(); m_send_que_lock.lock();
_dbg1("sleep for queue: " << ms); _dbg1("sleep for queue: " << ms);
if (m_was_shutdown)
return false;
if (retry > retry_limit) { if (retry > retry_limit) {
MWARNING("send que size is more than ABSTRACT_SERVER_SEND_QUE_MAX_COUNT(" << ABSTRACT_SERVER_SEND_QUE_MAX_COUNT << "), shutting down connection"); MWARNING("send que size is more than ABSTRACT_SERVER_SEND_QUE_MAX_COUNT(" << ABSTRACT_SERVER_SEND_QUE_MAX_COUNT << "), shutting down connection");
@ -748,7 +762,8 @@ PRAGMA_WARNING_DISABLE_VS(4355)
template<class t_protocol_handler> template<class t_protocol_handler>
void connection<t_protocol_handler>::reset_timer(boost::posix_time::milliseconds ms, bool add) void connection<t_protocol_handler>::reset_timer(boost::posix_time::milliseconds ms, bool add)
{ {
if (ms.total_milliseconds() < 0) const auto tms = ms.total_milliseconds();
if (tms < 0 || (add && tms == 0))
{ {
MWARNING("Ignoring negative timeout " << ms); MWARNING("Ignoring negative timeout " << ms);
return; return;