Implement IDisposable for IndexDownloader
MODIFY
WalletWasabi.Gui/Global.cs
WalletWasabi.Gui/Global.cs
@@ -86,10 +86,10 @@ namespace WalletWasabi.Gui
Logger.LogInfo("Start synchronizing filters...");
}
- public async static Task DisposeAsync()
+ public static void Dispose()
{
// Dispose index downloader service.
- IndexDownloader?.Stop();
+ IndexDownloader?.Dispose();
Logger.LogInfo($"{nameof(IndexDownloader)} is stopped.", nameof(Global));
Directory.CreateDirectory(Path.GetDirectoryName(AddressManagerFilePath));

MODIFY
WalletWasabi.Gui/Program.cs
WalletWasabi.Gui/Program.cs
@@ -44,7 +44,7 @@ namespace WalletWasabi.Gui
finally
{
statusBar?.Dispose();
- await Global.DisposeAsync();
+ Global.Dispose();
}
}

MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -218,8 +218,7 @@ namespace WalletWasabi.Tests
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(FilterDownloaderTestAsync), $"Index{rpc.Network}.dat");
- var downloader = new IndexDownloader(rpc.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
- try
+ using (var downloader = new IndexDownloader(rpc.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint)))
{
downloader.Synchronize(requestInterval: TimeSpan.FromSeconds(1));
@@ -265,10 +264,6 @@ namespace WalletWasabi.Tests
Assert.Null(filter.Filter);
}
}
- finally
- {
- downloader?.Stop();
- }
}
[Fact, TestPriority(7)]
@@ -295,7 +290,8 @@ namespace WalletWasabi.Tests
var node = RegTestFixture.BackendRegTestNode;
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(ReorgTestAsync), $"Index{rpc.Network}.dat");
- var downloader = new IndexDownloader(rpc.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
+ using (var downloader = new IndexDownloader(rpc.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint)))
+ {
try
{
downloader.Synchronize(requestInterval: TimeSpan.FromSeconds(3));
@@ -372,8 +368,7 @@ namespace WalletWasabi.Tests
finally
{
downloader.Reorged -= ReorgTestAsync_Downloader_Reorged;
-
- downloader?.Stop();
+ }
}
}
@@ -595,7 +590,7 @@ namespace WalletWasabi.Tests
wallet.NewFilterProcessed -= Wallet_NewFilterProcessed;
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose mempool service.
memPoolService.TransactionReceived -= WalletTestsAsync_MemPoolService_TransactionReceived;
@@ -1064,7 +1059,7 @@ namespace WalletWasabi.Tests
wallet.NewFilterProcessed -= Wallet_NewFilterProcessed;
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose connection service.
nodes?.Dispose();
// Dispose chaumian coinjoin client.
@@ -1241,7 +1236,7 @@ namespace WalletWasabi.Tests
{
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose connection service.
nodes?.Dispose();
// Dispose chaumian coinjoin client.
@@ -1409,7 +1404,7 @@ namespace WalletWasabi.Tests
{
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose connection service.
nodes?.Dispose();
// Dispose chaumian coinjoin client.
@@ -1561,7 +1556,7 @@ namespace WalletWasabi.Tests
{
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose connection service.
nodes?.Dispose();
// Dispose chaumian coinjoin client.
@@ -2801,7 +2796,7 @@ namespace WalletWasabi.Tests
wallet.NewFilterProcessed -= Wallet_NewFilterProcessed;
wallet?.Dispose();
// Dispose index downloader service.
- indexDownloader?.Stop();
+ indexDownloader?.Dispose();
// Dispose connection service.
nodes?.Dispose();
// Dispose chaumian coinjoin client.
@@ -2811,7 +2806,7 @@ namespace WalletWasabi.Tests
}
wallet2?.Dispose();
// Dispose index downloader service.
- indexDownloader2?.Stop();
+ indexDownloader2?.Dispose();
// Dispose connection service.
nodes2?.Dispose();
// Dispose chaumian coinjoin client.

MODIFY
WalletWasabi/Services/IndexDownloader.cs
WalletWasabi/Services/IndexDownloader.cs
@@ -17,7 +17,7 @@ using WalletWasabi.WebClients.ChaumianCoinJoin;
namespace WalletWasabi.Services
{
- public class IndexDownloader
+ public class IndexDownloader : IDisposable
{
public Network Network { get; }
@@ -231,22 +231,6 @@ namespace WalletWasabi.Services
}
}
- public void Stop()
- {
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
- Cancel?.Cancel();
- while (IsStopping)
- {
- Task.Delay(50).GetAwaiter().GetResult(); // DO NOT MAKE IT ASYNC (.NET Core threading brainfart)
- }
-
- Cancel?.Dispose();
- WasabiClient?.Dispose();
- }
-
public IEnumerable<FilterModel> GetFiltersIncluding(uint256 blockHash)
{
using (IndexLock.Lock())
@@ -286,5 +270,43 @@ namespace WalletWasabi.Services
}
}
}
+
+ #region IDisposable Support
+
+ private volatile bool _disposedValue = false; // To detect redundant calls
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!_disposedValue)
+ {
+ if (disposing)
+ {
+ if (IsRunning)
+ {
+ Interlocked.Exchange(ref _running, 2);
+ }
+ Cancel?.Cancel();
+ while (IsStopping)
+ {
+ Task.Delay(50).GetAwaiter().GetResult(); // DO NOT MAKE IT ASYNC (.NET Core threading brainfart)
+ }
+
+ Cancel?.Dispose();
+ WasabiClient?.Dispose();
+ }
+
+ _disposedValue = true;
+ }
+ }
+
+ // This code added to correctly implement the disposable pattern.
+ public void Dispose()
+ {
+ // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
+ Dispose(true);
+ // GC.SuppressFinalize(this);
+ }
+
+ #endregion IDisposable Support
}
}

Implement IDisposable for IndexDownloader