Synchronize InitializeNoWalletAsync & Dispose methods.
MODIFY
WalletWasabi.Gui/Global.cs
WalletWasabi.Gui/Global.cs
using Avalonia.Controls.Notifications;
using Microsoft.Extensions.Caching.Memory;
using NBitcoin;
+using Nito.AsyncEx;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@@ -118,15 +119,26 @@ namespace WalletWasabi.Gui
private TaskCompletionSource<bool> InitializationCompleted { get; } = new();
- private bool InitializationStarted { get; set; } = false;
+ private volatile bool _disposeRequested;
+ /// <summary>Lock that makes sure the application initialization and dispose methods do not run concurrently.</summary>
+ private AsyncLock InitializationAsyncLock { get; } = new();
+
+ /// <summary>Cancellation token to cancel <see cref="InitializeNoWalletAsync(TerminateService)"/> processing.</summary>
private CancellationTokenSource StoppingCts { get; } = new();
public async Task InitializeNoWalletAsync(TerminateService terminateService)
+ {
+ using (await InitializationAsyncLock.LockAsync())
{
Logger.LogTrace("Initialization started.");
- InitializationStarted = true;
- var cancel = StoppingCts.Token;
+
+ if (_disposeRequested)
+ {
+ return;
+ }
+
+ CancellationToken cancel = StoppingCts.Token;
try
{
@@ -203,6 +215,7 @@ namespace WalletWasabi.Gui
Logger.LogTrace("Initialization finished.");
}
}
+ }
private async Task StartRpcServerAsync(TerminateService terminateService, CancellationToken cancel)
{
@@ -439,18 +452,24 @@ namespace WalletWasabi.Gui
public async Task DisposeAsync()
{
- Logger.LogWarning("Process is exiting.", nameof(Global));
-
- try
+ // Dispose method may be called just once.
+ if (!_disposeRequested)
{
+ _disposeRequested = true;
StoppingCts.Cancel();
-
- if (!InitializationStarted)
+ }
+ else
{
return;
}
- Logger.LogDebug($"Waiting for initialization to complete.", nameof(Global));
+ using (await InitializationAsyncLock.LockAsync())
+ {
+ Logger.LogWarning("Process is exiting.", nameof(Global));
+
+ try
+ {
+ Logger.LogDebug("Waiting for initialization to complete.", nameof(Global));
try
{
@@ -567,3 +586,4 @@ namespace WalletWasabi.Gui
}
}
}
+}

Synchronize InitializeNoWalletAsync & Dispose methods.