Skip to content

Commit

Permalink
rpc: Make unloadwallet wait for complete wallet unload
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#14941
Rebased-From: c37851d
  • Loading branch information
promag authored and uhliksk committed May 1, 2019
1 parent 2eab86b commit 0edd154
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ void Shutdown()
LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what());
}
#endif
g_wallet_init_interface.Close();
UnregisterAllValidationInterfaces();
GetMainSignals().UnregisterBackgroundSignalScheduler();
GetMainSignals().UnregisterWithMempoolSignals(mempool);
g_wallet_init_interface.Close();
globalVerifyHandle.reset();
ECC_Stop();
LogPrintf("%s: done\n", __func__);
Expand Down
8 changes: 6 additions & 2 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ void WalletInit::Stop() const

void WalletInit::Close() const
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
RemoveWallet(pwallet);
auto wallets = GetWallets();
while (!wallets.empty()) {
auto wallet = wallets.back();
wallets.pop_back();
RemoveWallet(wallet);
UnloadWallet(std::move(wallet));
}
}
10 changes: 1 addition & 9 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3346,16 +3346,8 @@ static UniValue unloadwallet(const JSONRPCRequest& request)
if (!RemoveWallet(wallet)) {
throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded");
}
UnregisterValidationInterface(wallet.get());

// The wallet can be in use so it's not possible to explicitly unload here.
// Just notify the unload intent so that all shared pointers are released.
// The wallet will be destroyed once the last shared pointer is released.
wallet->NotifyUnload();

// There's no point in waiting for the wallet to unload.
// At this point this method should never fail. The unloading could only
// fail due to an unexpected error which would cause a process termination.
UnloadWallet(std::move(wallet));

return NullUniValue;
}
Expand Down
39 changes: 39 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,52 @@ std::shared_ptr<CWallet> GetWallet(const std::string& name)
return nullptr;
}

static std::mutex g_wallet_release_mutex;
static std::condition_variable g_wallet_release_cv;
static std::set<CWallet*> g_unloading_wallet_set;

// Custom deleter for shared_ptr<CWallet>.
static void ReleaseWallet(CWallet* wallet)
{
// Unregister and delete the wallet right after BlockUntilSyncedToCurrentChain
// so that it's in sync with the current chainstate.
wallet->WalletLogPrintf("Releasing wallet\n");
wallet->BlockUntilSyncedToCurrentChain();
wallet->Flush();
UnregisterValidationInterface(wallet);
delete wallet;
// Wallet is now released, notify UnloadWallet, if any.
{
std::lock_guard<std::mutex> lock(g_wallet_release_mutex);
if (g_unloading_wallet_set.erase(wallet) == 0) {
// UnloadWallet was not called for this wallet, all done.
return;
}
}
g_wallet_release_cv.notify_all();
}

void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
{
// Mark wallet for unloading.
CWallet* pwallet = wallet.get();
{
std::lock_guard<std::mutex> lock(g_wallet_release_mutex);
auto it = g_unloading_wallet_set.insert(pwallet);
assert(it.second);
}
// The wallet can be in use so it's not possible to explicitly unload here.
// Notify the unload intent so that all remaining shared pointers are
// released.
pwallet->NotifyUnload();
// Time to ditch our shared_ptr and wait for ReleaseWallet call.
wallet.reset();
{
std::unique_lock<std::mutex> lock(g_wallet_release_mutex);
while (g_unloading_wallet_set.count(pwallet) == 1) {
g_wallet_release_cv.wait(lock);
}
}
}

const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
Expand Down
9 changes: 9 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
#include <utility>
#include <vector>

//! Explicitly unload and delete the wallet.
// Blocks the current thread after signaling the unload intent so that all
// wallet clients release the wallet.
// Note that, when blocking is not required, the wallet is implicitly unloaded
// by the shared pointer deleter.
void UnloadWallet(std::shared_ptr<CWallet>&& wallet);

bool AddWallet(const std::shared_ptr<CWallet>& wallet);
bool RemoveWallet(const std::shared_ptr<CWallet>& wallet);
bool HasWallets();
Expand Down Expand Up @@ -929,6 +936,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface

~CWallet()
{
// Should not have slots connected at this point.
assert(NotifyUnload.empty());
delete encrypted_batch;
encrypted_batch = nullptr;
}
Expand Down

0 comments on commit 0edd154

Please sign in to comment.