Use DisconnectAsync
MODIFY
WalletWasabi/BitcoinCore/Monitoring/P2pReconnector.cs
WalletWasabi/BitcoinCore/Monitoring/P2pReconnector.cs
@@ -23,12 +23,12 @@ namespace WalletWasabi.BitcoinCore.Monitoring
try
{
Logger.LogInfo("Trying to reconnect to P2P...");
- P2pNode.Disconnect();
+ await P2pNode.DisconnectAsync(cancel).ConfigureAwait(false);
await P2pNode.ConnectAsync(cancel).ConfigureAwait(false);
}
catch
{
- P2pNode.Disconnect();
+ await P2pNode.DisconnectAsync(cancel);
throw;
}

MODIFY
WalletWasabi/BitcoinCore/P2pNode.cs
WalletWasabi/BitcoinCore/P2pNode.cs
@@ -2,6 +2,7 @@ using NBitcoin;
using NBitcoin.Protocol;
using Nito.AsyncEx;
using System;
+using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
@@ -130,7 +131,7 @@ namespace WalletWasabi.BitcoinCore
{
Stop?.Cancel();
await ReconnectorTask.ConfigureAwait(false);
- Disconnect();
+ await DisconnectAsync(CancellationToken.None);
Stop?.Dispose();
Stop = null;
}
@@ -138,7 +139,7 @@ namespace WalletWasabi.BitcoinCore
/// <summary>
/// It is not equivalent to Dispose, but it is being called from Dispose.
/// </summary>
- public void Disconnect()
+ public async Task DisconnectAsync(CancellationToken cancel)
{
Node node = Node;
if (node is { })
@@ -162,29 +163,27 @@ namespace WalletWasabi.BitcoinCore
try
{
- node.Disconnect();
- }
- catch (Exception ex)
- {
- Logger.LogDebug(ex);
- }
- finally
+ // Disconnection not waited here.
+ node.DisconnectAsync();
+
+ var sw = Stopwatch.StartNew();
+ while (node.IsConnected)
{
- try
+ await Task.Delay(50, cancel).ConfigureAwait(false);
+ if (sw.Elapsed > TimeSpan.FromSeconds(20))
{
- node.Dispose();
+ throw new TimeoutException("Could not disconnect P2p Bitcoin node");
+ }
+ }
}
catch (Exception ex)
{
Logger.LogDebug(ex);
}
- finally
- {
+
Node = null;
Logger.LogInfo("P2p Bitcoin node is disconnected.");
}
}
}
}
- }
-}

Use DisconnectAsync