Refactor disposing in startup
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -165,7 +165,35 @@ public class Global : IDisposable
{
if (disposing)
{
- // TODO: dispose managed state (managed objects)
+ if (Coordinator is { } coordinator)
+ {
+ coordinator.Dispose();
+ Logger.LogInfo($"{nameof(coordinator)} is disposed.");
+ }
+
+ var stoppingTask = Task.Run(async () =>
+ {
+ if (IndexBuilderService is { } indexBuilderService)
+ {
+ await indexBuilderService.StopAsync();
+ Logger.LogInfo($"{nameof(indexBuilderService)} is stopped.");
+ }
+
+ if (HostedServices is { } hostedServices)
+ {
+ using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(21));
+ await hostedServices.StopAllAsync(cts.Token);
+ hostedServices.Dispose();
+ }
+
+ if (P2pNode is { } p2pNode)
+ {
+ await p2pNode.DisposeAsync();
+ Logger.LogInfo($"{nameof(p2pNode)} is disposed.");
+ }
+ });
+
+ stoppingTask.GetAwaiter().GetResult();
}
_disposedValue = true;

MODIFY
WalletWasabi.Backend/Startup.cs
WalletWasabi.Backend/Startup.cs
@@ -114,40 +114,7 @@ public class Startup
private void OnShutdown(Global global)
{
- CleanupAsync(global).GetAwaiter().GetResult(); // This is needed, if async function is registered then it won't wait until it finishes
- }
-
- private async Task CleanupAsync(Global global)
- {
- var coordinator = global.Coordinator;
- if (coordinator is { })
- {
- coordinator.Dispose();
- Logger.LogInfo($"{nameof(coordinator)} is disposed.");
- }
-
- var indexBuilderService = global.IndexBuilderService;
- if (indexBuilderService is { })
- {
- await indexBuilderService.StopAsync();
- Logger.LogInfo($"{nameof(indexBuilderService)} is stopped.");
- }
-
- var hostedServices = global.HostedServices;
- if (hostedServices is { })
- {
- using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(21));
- await hostedServices.StopAllAsync(cts.Token);
- hostedServices.Dispose();
- }
-
- var p2pNode = global.P2pNode;
- if (p2pNode is { })
- {
- await p2pNode.DisposeAsync();
- Logger.LogInfo($"{nameof(p2pNode)} is disposed.");
- }
-
+ global.Dispose();
Logger.LogSoftwareStopped("Wasabi Backend");
}
}

Refactor disposing in startup