aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoão Barbosa <joao.paulo.barbosa@gmail.com>2018-10-10 00:32:27 +0100
committerJoão Barbosa <joao.paulo.barbosa@gmail.com>2018-12-05 23:14:10 +0000
commit75b5d8c4ead9c41f08fe53f9fffd3ffc984d6684 (patch)
tree482970be8585033416454a9a4eb08d7d1fb226f4 /src
parent5d12143c730c3dcd11bb9dce16ed43a15a16eea2 (diff)
downloadbitcoin-75b5d8c4ead9c41f08fe53f9fffd3ffc984d6684.tar.xz
rpc: Fix wallet unload during walletpassphrase timeout
Github-Pull: #14453 Rebased-From: 321decf
Diffstat (limited to 'src')
-rw-r--r--src/wallet/rpcwallet.cpp20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index 6400b4470f..193da76551 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -2546,13 +2546,6 @@ static UniValue keypoolrefill(const JSONRPCRequest& request)
}
-static void LockWallet(CWallet* pWallet)
-{
- LOCK(pWallet->cs_wallet);
- pWallet->nRelockTime = 0;
- pWallet->Lock();
-}
-
static UniValue walletpassphrase(const JSONRPCRequest& request)
{
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -2622,7 +2615,18 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
pwallet->TopUpKeyPool();
pwallet->nRelockTime = GetTime() + nSleepTime;
- RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), std::bind(LockWallet, pwallet), nSleepTime);
+
+ // Keep a weak pointer to the wallet so that it is possible to unload the
+ // wallet before the following callback is called. If a valid shared pointer
+ // is acquired in the callback then the wallet is still loaded.
+ std::weak_ptr<CWallet> weak_wallet = wallet;
+ RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet] {
+ if (auto shared_wallet = weak_wallet.lock()) {
+ LOCK(shared_wallet->cs_wallet);
+ shared_wallet->Lock();
+ shared_wallet->nRelockTime = 0;
+ }
+ }, nSleepTime);
return NullUniValue;
}