aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2018-12-06 13:07:21 -0500
committerMarcoFalke <falke.marco@gmail.com>2018-12-06 13:07:25 -0500
commitabae8aeff1cb3e501aa28e58d82254afc72c4e66 (patch)
tree5518f0980754997c94b2cce94763aed5e0683621
parent8b8b3a9a5fd9d65d8ed0933352fad808327e0bf3 (diff)
parentdcb032dcdf8e8300a84ae8ec6cfde0da5381f56f (diff)
downloadbitcoin-abae8aeff1cb3e501aa28e58d82254afc72c4e66.tar.xz
Merge #14880: 0.17: Backport #14453
dcb032dcdf qa: Ensure wallet unload during walletpassphrase timeout (João Barbosa) 75b5d8c4ea rpc: Fix wallet unload during walletpassphrase timeout (João Barbosa) Pull request description: Backport #14453 to 0.17 Tree-SHA512: fce0adccbb07b6635bb773a71beb4a9b814bceb77bbe7bbc5bcb7d151aabb1148c791622f58c990afe202012ca1971cd466cb536fc6f37e22cdc58738720b593
-rw-r--r--src/wallet/rpcwallet.cpp20
-rwxr-xr-xtest/functional/wallet_multiwallet.py9
2 files changed, 21 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;
}
diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py
index 435821ec48..f53172639c 100755
--- a/test/functional/wallet_multiwallet.py
+++ b/test/functional/wallet_multiwallet.py
@@ -8,6 +8,7 @@ Verify that a bitcoind node can load multiple wallet files
"""
import os
import shutil
+import time
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch
@@ -262,7 +263,15 @@ class MultiWalletTest(BitcoinTestFramework):
assert 'w1' not in self.nodes[0].listwallets()
# Successfully unload the wallet referenced by the request endpoint
+ # Also ensure unload works during walletpassphrase timeout
+ wallets = node.listwallets()
+ w2.encryptwallet('test')
+ self.restart_node(0, ['-wallet={}'.format(wallet) for wallet in wallets])
+ w1 = node.get_wallet_rpc(wallet_names[0])
+ w2 = node.get_wallet_rpc(wallet_names[1])
+ w2.walletpassphrase('test', 1)
w2.unloadwallet()
+ time.sleep(1.1)
assert 'w2' not in self.nodes[0].listwallets()
# Successfully unload all wallets