From 29aa2a25def1257a246fe22c621a6c3bfbf9718c Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 14 May 2020 19:23:17 +0200 Subject: [PATCH] added wallets_manager deinitialization before everyone dies --- src/wallet/plain_wallet_api.cpp | 29 ++++++++++++++++++----------- src/wallet/wallets_manager.cpp | 18 +++++++++++++----- src/wallet/wallets_manager.h | 3 ++- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 2fd82ad3..30413acc 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -42,9 +42,22 @@ struct plain_wallet_instance std::shared_ptr ginstance_ptr; +#define GET_INSTANCE_PTR(ptr_name) \ + auto ptr_name = std::atomic_load(&ginstance_ptr); \ + if (!ptr_name) \ + { \ + LOG_ERROR("Core already deinitialised or not initialized yet."); \ + epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); \ + ok_response.result.return_code = API_RETURN_CODE_UNINITIALIZED; \ + return epee::serialization::store_t_to_json(ok_response); \ + } + +void deinit(); epee::misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler([]() { - std::cout << "[LEAVE HANDLER CALLED]" << ENDL; + std::cout << "[BEFORE DEINIT]" << ENDL; + deinit(); + std::cout << "[AFTER DEINIT]" << ENDL; }); namespace plain_wallet @@ -132,7 +145,11 @@ namespace plain_wallet //wait other callers finish local_ptr->gjobs_lock.lock(); local_ptr->gjobs_lock.unlock(); + bool r = local_ptr->gwm.quick_stop_no_save(); + std::cout << "[QUICK_STOP_NO_SAVE] return " << r; + //let's prepare wallet manager for quick shutdown local_ptr.reset(); + } } @@ -252,16 +269,6 @@ namespace plain_wallet return epee::serialization::store_t_to_json(ok_response); } -#define GET_INSTANCE_PTR(ptr_name) \ - auto ptr_name = std::atomic_load(&ginstance_ptr); \ - if (!ptr_name) \ - { \ - LOG_ERROR("Core already deinitialised or not initialized yet."); \ - epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); \ - ok_response.result.return_code = API_RETURN_CODE_UNINITIALIZED; \ - return epee::serialization::store_t_to_json(ok_response); \ - } - std::string get_connectivity_status() { GET_INSTANCE_PTR(inst_ptr); diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 79e979eb..3ab5ceb1 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -56,7 +56,8 @@ wallets_manager::wallets_manager():m_pview(&m_view_stub), m_ui_opt(AUTO_VAL_INIT(m_ui_opt)), m_remote_node_mode(false), m_is_pos_allowed(false), - m_qt_logs_enbaled(false) + m_qt_logs_enbaled(false), + dont_save_wallet_at_stop(false) { #ifndef MOBILE_WALLET_BUILD m_offers_service.set_disabled(true); @@ -87,6 +88,7 @@ wallets_manager::~wallets_manager() { TRY_ENTRY(); stop(); + LOG_LEVEL_0("[~WALLETS_MANAGER] destroyed"); CATCH_ENTRY_NO_RETURN(); } @@ -297,12 +299,17 @@ bool wallets_manager::stop() LOG_PRINT_L0("Waiting for backend main worker thread: " << m_main_worker_thread.get_id()); m_main_worker_thread.join(); } - - - + return true; } + +bool wallets_manager::quick_stop_no_save() //stop without storing wallets +{ + dont_save_wallet_at_stop = true; + return stop(); +} + std::string wallets_manager::get_config_folder() { return m_data_dir; @@ -508,7 +515,8 @@ void wallets_manager::main_worker(const po::variables_map& m_vm) wo.second.stop_for_refresh = true; wo.second.w.unlocked_get()->stop(); - wo.second.w->get()->store(); + if(!dont_save_wallet_at_stop) + wo.second.w->get()->store(); } catch (const std::exception& e) { diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 34d55b00..aa900d93 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -71,7 +71,6 @@ public: std::atomic* plast_daemon_is_disconnected; std::atomic has_related_alias_in_unconfirmed; std::atomic need_to_update_wallet_info; - std::atomic long_refresh_in_progress; epee::critical_section long_refresh_in_progress_lock; //secure wallet state and prevent from long wait while long refresh is in work @@ -90,6 +89,7 @@ public: bool init(int argc, char* argv[], view::i_view* pview_handler); bool start(); bool stop(); + bool quick_stop_no_save(); //stop without storing wallets bool send_stop_signal(); std::string open_wallet(const std::wstring& path, const std::string& password, uint64_t txs_to_return, view::open_wallet_response& owr); std::string generate_wallet(const std::wstring& path, const std::string& password, view::open_wallet_response& owr); @@ -184,6 +184,7 @@ private: std::atomic m_last_daemon_is_disconnected; // std::atomic m_last_wallet_synch_height; std::atomic m_wallet_id_counter; + std::atomic dont_save_wallet_at_stop; std::string m_data_dir; view::gui_options m_ui_opt;