Browse Source

Cancel network read on stop

v0.17.1.9-feather
tobtoht 4 months ago
parent
commit
4132797411
Signed by: tobtoht GPG Key ID: 1CADD27F41F45C3C
  1. 3
      CMakeLists.txt
  2. 1
      contrib/epee/include/net/abstract_http_client.h
  3. 5
      contrib/epee/include/net/http_client.h
  4. 25
      contrib/epee/include/net/net_helper.h
  5. 2
      src/wallet/wallet2.cpp
  6. 6
      src/wallet/wallet2.h

3
CMakeLists.txt

@ -797,7 +797,8 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default -DGTEST_HAS_TR1_TUPLE=0")
endif()
set(DEBUG_FLAGS "-g3")
# gdb doesn't like -g3, can cause source files to not get detected
set(DEBUG_FLAGS "-g")
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8))
set(DEBUG_FLAGS "${DEBUG_FLAGS} -Og ")
else()

1
contrib/epee/include/net/abstract_http_client.h

@ -69,6 +69,7 @@ namespace http
virtual void set_auto_connect(bool auto_connect) = 0;
virtual bool connect(std::chrono::milliseconds timeout) = 0;
virtual bool disconnect() = 0;
virtual bool cancel_read() = 0;
virtual bool is_connected(bool *ssl = NULL) = 0;
virtual bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0;
virtual bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string& body = std::string(), const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0;

5
contrib/epee/include/net/http_client.h

@ -206,6 +206,11 @@ namespace net_utils
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.disconnect();
}
//---------------------------------------------------------------------------
bool cancel_read() override
{
return m_net_client.cancel_read();
}
//---------------------------------------------------------------------------
bool is_connected(bool *ssl = NULL) override
{

25
contrib/epee/include/net/net_helper.h

@ -110,6 +110,7 @@ namespace net_utils
m_connected(false),
m_deadline(m_io_service, std::chrono::steady_clock::time_point::max()),
m_shutdowned(0),
m_cancel_read(0),
m_bytes_sent(0),
m_bytes_received(0)
{
@ -265,14 +266,14 @@ namespace net_utils
m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
}
}
catch(const boost::system::system_error& /*er*/)
catch(const boost::system::system_error& er)
{
//LOG_ERROR("Some problems at disconnect, message: " << er.what());
LOG_ERROR("Some problems at disconnect, message: " << er.what());
return false;
}
catch(...)
{
//LOG_ERROR("Some fatal problems.");
LOG_ERROR("Some fatal problems.");
return false;
}
return true;
@ -440,8 +441,14 @@ namespace net_utils
m_io_service.run_one();
}
if (boost::interprocess::ipcdetail::atomic_read32(&m_cancel_read))
{
LOG_ERROR("Cancelling read, returning false");
m_connected = false;
return false;
}
if (ec)
if (ec)
{
MTRACE("READ ENDS: Connection err_code " << ec.value());
if(ec == boost::asio::error::eof)
@ -579,7 +586,14 @@ namespace net_utils
m_connected = false;
return true;
}
bool cancel_read()
{
MDEBUG("Cancelling read");
boost::interprocess::ipcdetail::atomic_write32(&m_cancel_read, 1);
return true;
}
boost::asio::io_service& get_io_service()
{
return m_io_service;
@ -685,6 +699,7 @@ namespace net_utils
bool m_connected;
boost::asio::steady_timer m_deadline;
volatile uint32_t m_shutdowned;
volatile uint32_t m_cancel_read;
std::atomic<uint64_t> m_bytes_sent;
std::atomic<uint64_t> m_bytes_received;
};

2
src/wallet/wallet2.cpp

@ -3566,7 +3566,7 @@ void wallet2::refresh(bool trusted_daemon, uint64_t start_height, uint64_t & blo
{
blocks_fetched += added_blocks;
THROW_WALLET_EXCEPTION_IF(!waiter.wait(), error::wallet_internal_error, "Exception in thread pool");
if(try_count < 3)
if(try_count < 3 && m_run.load(std::memory_order_relaxed)) // Don't retry if we want to stop refreshing
{
LOG_PRINT_L1("Another try pull_blocks (try_count=" << try_count << ")...");
first = true;

6
src/wallet/wallet2.h

@ -888,7 +888,11 @@ private:
epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_autodetect);
bool set_proxy(const std::string &address);
void stop() { m_run.store(false, std::memory_order_relaxed); m_message_store.stop(); }
void stop() {
m_run.store(false, std::memory_order_relaxed);
m_message_store.stop();
m_http_client->cancel_read();
}
i_wallet2_callback* callback() const { return m_callback; }
void callback(i_wallet2_callback* callback) { m_callback = callback; }

Loading…
Cancel
Save