@@ -437,13 +437,20 @@ static RPCHelpMan unloadwallet()
437437 throw JSONRPCError (RPC_WALLET_NOT_FOUND, " Requested wallet does not exist or is not loaded" );
438438 }
439439
440- // Release the "main" shared pointer and prevent further notifications.
441- // Note that any attempt to load the same wallet would fail until the wallet
442- // is destroyed (see CheckUniqueFileid).
443440 std::vector<bilingual_str> warnings;
444- std::optional<bool > load_on_start = request.params [1 ].isNull () ? std::nullopt : std::optional<bool >(request.params [1 ].get_bool ());
445- if (!RemoveWallet (context, wallet, load_on_start, warnings)) {
446- throw JSONRPCError (RPC_MISC_ERROR, " Requested wallet already unloaded" );
441+ {
442+ WalletRescanReserver reserver (*wallet);
443+ if (!reserver.reserve ()) {
444+ throw JSONRPCError (RPC_WALLET_ERROR, " Wallet is currently rescanning. Abort existing rescan or wait." );
445+ }
446+
447+ // Release the "main" shared pointer and prevent further notifications.
448+ // Note that any attempt to load the same wallet would fail until the wallet
449+ // is destroyed (see CheckUniqueFileid).
450+ std::optional<bool > load_on_start = request.params [1 ].isNull () ? std::nullopt : std::optional<bool >(request.params [1 ].get_bool ());
451+ if (!RemoveWallet (context, wallet, load_on_start, warnings)) {
452+ throw JSONRPCError (RPC_MISC_ERROR, " Requested wallet already unloaded" );
453+ }
447454 }
448455
449456 UnloadWallet (std::move (wallet));
0 commit comments