Fix unloading all wallets on error
MODIFY
WalletWasabi.Gui/Tabs/WalletManager/LoadWalletViewModel.cs
WalletWasabi.Gui/Tabs/WalletManager/LoadWalletViewModel.cs
@@ -659,8 +659,6 @@ namespace WalletWasabi.Gui.Tabs.WalletManager
{
Logger.LogError(ex);
}
-
- await Global.WalletManager.RemoveAndStopAllAsync().ConfigureAwait(false);
}
}
finally

MODIFY
WalletWasabi/Wallets/WalletManager.cs
WalletWasabi/Wallets/WalletManager.cs
@@ -31,9 +31,10 @@ namespace WalletWasabi.Wallets
Wallets = new Dictionary<WalletService, HashSet<uint256>>();
Lock = new object();
AddRemoveLock = new AsyncLock();
+ CancelAllInitialization = new CancellationTokenSource();
}
- private CancellationTokenSource CancelWalletServiceInitialization { get; set; } = null;
+ private CancellationTokenSource CancelAllInitialization { get; }
public event EventHandler<ProcessedResult> WalletRelevantTransactionProcessed;
@@ -64,11 +65,14 @@ namespace WalletWasabi.Wallets
public async Task<WalletService> CreateAndStartWalletServiceAsync(KeyManager keyManager)
{
- WalletService walletService = new WalletService(BitcoinStore, keyManager, Synchronizer, Nodes, DataDir, ServiceConfiguration, FeeProvider, BitcoinCoreNode);
- using (CancelWalletServiceInitialization = new CancellationTokenSource())
using (await AddRemoveLock.LockAsync().ConfigureAwait(false))
{
- var cancel = CancelWalletServiceInitialization.Token;
+ WalletService walletService = null;
+ try
+ {
+ walletService = new WalletService(BitcoinStore, keyManager, Synchronizer, Nodes, DataDir, ServiceConfiguration, FeeProvider, BitcoinCoreNode);
+
+ var cancel = CancelAllInitialization.Token;
lock (Lock)
{
Wallets.Add(walletService, new HashSet<uint256>());
@@ -82,12 +86,25 @@ namespace WalletWasabi.Wallets
walletService.TransactionProcessor.WalletRelevantTransactionProcessed += TransactionProcessor_WalletRelevantTransactionProcessed;
walletService.ChaumianClient.OnDequeue += ChaumianClient_OnDequeue;
- }
-
- CancelWalletServiceInitialization = null; // Must make it null explicitly, because dispose won't make it null.
return walletService;
}
+ catch (Exception)
+ {
+ if (walletService is { })
+ {
+ walletService.TransactionProcessor.WalletRelevantTransactionProcessed -= TransactionProcessor_WalletRelevantTransactionProcessed;
+ walletService.ChaumianClient.OnDequeue -= ChaumianClient_OnDequeue;
+ lock (Lock)
+ {
+ Wallets.Remove(walletService);
+ }
+ await walletService.StopAsync(CancellationToken.None).ConfigureAwait(false);
+ }
+ throw;
+ }
+ }
+ }
private void ChaumianClient_OnDequeue(object sender, DequeueResult e)
{
@@ -105,13 +122,13 @@ namespace WalletWasabi.Wallets
{
try
{
- CancelWalletServiceInitialization?.Cancel();
+ CancelAllInitialization?.Cancel();
+ CancelAllInitialization?.Dispose();
}
catch (ObjectDisposedException)
{
- Logger.LogWarning($"{nameof(CancelWalletServiceInitialization)} is disposed. This can occur due to an error while processing the wallet.");
+ Logger.LogWarning($"{nameof(CancelAllInitialization)} is disposed. This can occur due to an error while processing the wallet.");
}
- CancelWalletServiceInitialization = null;
using (await AddRemoveLock.LockAsync().ConfigureAwait(false))
{

Fix unloading all wallets on error