Skip to content

Commit 321decf

Browse files
committed
rpc: Fix wallet unload during walletpassphrase timeout
1 parent 1d14174 commit 321decf

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,13 +1928,6 @@ static UniValue keypoolrefill(const JSONRPCRequest& request)
19281928
}
19291929

19301930

1931-
static void LockWallet(CWallet* pWallet)
1932-
{
1933-
LOCK(pWallet->cs_wallet);
1934-
pWallet->nRelockTime = 0;
1935-
pWallet->Lock();
1936-
}
1937-
19381931
static UniValue walletpassphrase(const JSONRPCRequest& request)
19391932
{
19401933
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -2004,7 +1997,18 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
20041997
pwallet->TopUpKeyPool();
20051998

20061999
pwallet->nRelockTime = GetTime() + nSleepTime;
2007-
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), std::bind(LockWallet, pwallet), nSleepTime);
2000+
2001+
// Keep a weak pointer to the wallet so that it is possible to unload the
2002+
// wallet before the following callback is called. If a valid shared pointer
2003+
// is acquired in the callback then the wallet is still loaded.
2004+
std::weak_ptr<CWallet> weak_wallet = wallet;
2005+
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet] {
2006+
if (auto shared_wallet = weak_wallet.lock()) {
2007+
LOCK(shared_wallet->cs_wallet);
2008+
shared_wallet->Lock();
2009+
shared_wallet->nRelockTime = 0;
2010+
}
2011+
}, nSleepTime);
20082012

20092013
return NullUniValue;
20102014
}

0 commit comments

Comments
 (0)