|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
5 | 5 | #include <rpc/util.h>
|
| 6 | +#include <scheduler.h> |
| 7 | +#include <wallet/context.h> |
6 | 8 | #include <wallet/rpc/util.h>
|
7 | 9 | #include <wallet/wallet.h>
|
8 | 10 |
|
@@ -88,24 +90,24 @@ RPCHelpMan walletpassphrase()
|
88 | 90 | relock_time = pwallet->nRelockTime;
|
89 | 91 | }
|
90 | 92 |
|
91 |
| - // rpcRunLater must be called without cs_wallet held otherwise a deadlock |
92 |
| - // can occur. The deadlock would happen when RPCRunLater removes the |
93 |
| - // previous timer (and waits for the callback to finish if already running) |
94 |
| - // and the callback locks cs_wallet. |
95 |
| - AssertLockNotHeld(wallet->cs_wallet); |
| 93 | + // Get wallet scheduler to queue up the relock callback in the future. |
| 94 | + // Scheduled events don't get destructed until they are executed, |
| 95 | + // and they are executed in series in a single scheduler thread so |
| 96 | + // no cs_wallet lock is needed. |
| 97 | + WalletContext& context = EnsureWalletContext(request.context); |
96 | 98 | // Keep a weak pointer to the wallet so that it is possible to unload the
|
97 | 99 | // wallet before the following callback is called. If a valid shared pointer
|
98 | 100 | // is acquired in the callback then the wallet is still loaded.
|
99 | 101 | std::weak_ptr<CWallet> weak_wallet = wallet;
|
100 |
| - pwallet->chain().rpcRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet, relock_time] { |
| 102 | + context.scheduler->scheduleFromNow([weak_wallet, relock_time] { |
101 | 103 | if (auto shared_wallet = weak_wallet.lock()) {
|
102 | 104 | LOCK2(shared_wallet->m_relock_mutex, shared_wallet->cs_wallet);
|
103 |
| - // Skip if this is not the most recent rpcRunLater callback. |
| 105 | + // Skip if this is not the most recent relock callback. |
104 | 106 | if (shared_wallet->nRelockTime != relock_time) return;
|
105 | 107 | shared_wallet->Lock();
|
106 | 108 | shared_wallet->nRelockTime = 0;
|
107 | 109 | }
|
108 |
| - }, nSleepTime); |
| 110 | + }, std::chrono::seconds(nSleepTime)); |
109 | 111 |
|
110 | 112 | return UniValue::VNULL;
|
111 | 113 | },
|
|
0 commit comments