diff --git a/src/wallet.rs b/src/wallet.rs index f050d35dc6..2b4cf7685b 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -555,11 +555,29 @@ impl Wallet { }); } - settings + match settings .bitcoin_rpc_client(Some(name.clone()))? - .call::("importdescriptors", &[serde_json::to_value(descriptors)?])?; - - Ok(()) + .call::( + "importdescriptors", + &[serde_json::to_value(descriptors.clone())?], + ) { + Ok(_) => Ok(()), + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -4 && err.message == "Wallet already loading." => + { + // wallet loading + Ok(()) + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -35 => + { + // wallet already loaded + Ok(()) + } + Err(err) => { + bail!("Failed to import descriptors for wallet {}: {err}", name) + } + } } pub(crate) fn check_version(client: Client) -> Result { diff --git a/src/wallet/wallet_constructor.rs b/src/wallet/wallet_constructor.rs index d9d60fce5a..cdc559cda8 100644 --- a/src/wallet/wallet_constructor.rs +++ b/src/wallet/wallet_constructor.rs @@ -52,7 +52,29 @@ impl WalletConstructor { Wallet::check_version(self.settings.bitcoin_rpc_client(Some(self.name.clone()))?)?; if !client.list_wallets()?.contains(&self.name) { - client.load_wallet(&self.name)?; + loop { + match client.load_wallet(&self.name) { + Ok(_) => { + break; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -4 && err.message == "Wallet already loading." => + { + // wallet loading + thread::sleep(Duration::from_secs(3)); + continue; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -35 => + { + // wallet already loaded + break; + } + Err(err) => { + bail!("Failed to load wallet {}: {err}", self.name); + } + } + } } if client.get_wallet_info()?.private_keys_enabled {