Skip to content

Commit

Permalink
The thread running nano::wallets::do_wallet_actions may need to obtai…
Browse files Browse the repository at this point in the history
…n a shared_ptr to the node. This is not available until after the constructor completes, causing a race condition on startup which can lead to a crash. (#3568)

This adds nano::wallets::start() which does a deferred start from nano::node::start ().
  • Loading branch information
clemahieu authored Nov 23, 2021
1 parent 92c0d31 commit 15a4da3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ void nano::node::start ()
{
port_mapping.start ();
}
wallets.start ();
if (config.frontiers_confirmation != nano::frontiers_confirmation_mode::disabled)
{
workers.push_task ([this_l = shared ()] () {
Expand Down
14 changes: 9 additions & 5 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,7 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) :
kdf{ node_a.config.network_params.kdf_work },
node (node_a),
env (boost::polymorphic_downcast<nano::mdb_wallets_store *> (node_a.wallets_store_impl.get ())->environment),
stopped (false),
thread ([this] () {
nano::thread_role::set (nano::thread_role::name::wallet_actions);
do_wallet_actions ();
})
stopped (false)
{
nano::unique_lock<nano::mutex> lock (mutex);
if (!error_a)
Expand Down Expand Up @@ -1606,6 +1602,14 @@ void nano::wallets::stop ()
}
}

void nano::wallets::start ()
{
thread = std::thread{ [this] () {
nano::thread_role::set (nano::thread_role::name::wallet_actions);
do_wallet_actions ();
} };
}

nano::write_transaction nano::wallets::tx_begin_write ()
{
return env.tx_begin_write ();
Expand Down
1 change: 1 addition & 0 deletions nano/node/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class wallets final
void queue_wallet_action (nano::uint128_t const &, std::shared_ptr<nano::wallet> const &, std::function<void (nano::wallet &)>);
void foreach_representative (std::function<void (nano::public_key const &, nano::raw_key const &)> const &);
bool exists (nano::transaction const &, nano::account const &);
void start ();
void stop ();
void clear_send_ids (nano::transaction const &);
nano::wallet_representatives reps () const;
Expand Down

0 comments on commit 15a4da3

Please sign in to comment.