Skip to content

Commit c7500f4

Browse files
Fix connection manager
* Remove `m_is_polling` * Set atomic `m_auto_switch` and `m_timeout` * Make `PyThreadPoller::run_poll_loop` more robust
1 parent 1e36a8c commit c7500f4

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

src/cpp/common/py_monero_common.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void PyThreadPoller::set_is_polling(bool is_polling) {
3636
m_poll_cv.notify_one();
3737
// TODO: in emscripten, m_sync_cv.notify_one() returns without waiting, so sleep; bug in emscripten upstream llvm?
3838
std::this_thread::sleep_for(std::chrono::milliseconds(1));
39-
m_thread.join();
39+
if (m_thread.joinable()) m_thread.join();
4040
}
4141
}
4242
}
@@ -46,8 +46,7 @@ void PyThreadPoller::set_period_in_ms(uint64_t period_ms) {
4646
}
4747

4848
void PyThreadPoller::run_poll_loop() {
49-
if (m_poll_loop_running) return; // only run one loop at a time
50-
m_poll_loop_running = true;
49+
if (m_poll_loop_running.exchange(true)) return; // only run one loop at a time
5150

5251
// start pool loop thread
5352
// TODO: use global threadpool, background sync wasm wallet in c++ thread
@@ -63,11 +62,11 @@ void PyThreadPoller::run_poll_loop() {
6362
if (m_is_polling) {
6463
boost::mutex::scoped_lock lock(m_polling_mutex);
6564
boost::posix_time::milliseconds wait_for_ms(m_poll_period_ms.load());
66-
m_poll_cv.timed_wait(lock, wait_for_ms);
65+
m_poll_cv.timed_wait(lock, wait_for_ms, [&]() { return !m_is_polling; });
6766
}
6867
}
6968

70-
m_poll_loop_running = false;
69+
m_poll_loop_running.exchange(false);
7170
});
7271
}
7372

@@ -725,7 +724,7 @@ void PyMoneroConnectionManager::check_connection() {
725724
std::shared_ptr<PyMoneroRpcConnection> connection = get_connection();
726725
if (connection != nullptr) {
727726
// check current connection
728-
if (connection->check_connection(m_timeout)) connection_changed = true;
727+
if (connection->check_connection(m_timeout.load())) connection_changed = true;
729728
std::vector<std::shared_ptr<PyMoneroRpcConnection>> cons;
730729
cons.push_back(connection);
731730
process_responses(cons);
@@ -794,7 +793,7 @@ std::shared_ptr<PyMoneroRpcConnection> PyMoneroConnectionManager::get_best_avail
794793
remaining++;
795794

796795
boost::asio::post(pool, [&, connection]() {
797-
connection->check_connection(m_timeout);
796+
connection->check_connection(m_timeout.load());
798797

799798
{
800799
boost::lock_guard<boost::mutex> lock(mtx);
@@ -939,7 +938,7 @@ bool PyMoneroConnectionManager::check_connections(const std::vector<std::shared_
939938
num_tasks++;
940939

941940
boost::asio::post(pool, [this, connection, &result_mutex, &result_cv, &completed]() {
942-
bool change = connection->check_connection(m_timeout);
941+
bool change = connection->check_connection(m_timeout.load());
943942

944943
if (change && connection == get_connection()) {
945944
on_connection_changed(connection);

src/cpp/common/py_monero_common.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,8 @@ class PyMoneroConnectionManager : public PyThreadPoller {
775775
std::shared_ptr<PyMoneroRpcConnection> m_current_connection;
776776
std::set<std::shared_ptr<PyMoneroRpcConnection>> m_excluded_connections;
777777

778-
bool m_is_polling = false;
779-
bool m_auto_switch = true;
780-
uint64_t m_timeout = 5000;
778+
std::atomic<bool> m_auto_switch = true;
779+
std::atomic<uint64_t> m_timeout = 5000;
781780

782781
std::map<std::shared_ptr<PyMoneroRpcConnection>, std::vector<boost::optional<long>>> m_response_times;
783782

0 commit comments

Comments
 (0)