TorProcessManager: Improve documentation, rename `Stop` to `MonitorCts`, add dispose for `TorSocks5Client`.
MODIFY
WalletWasabi/Tor/TorProcessManager.cs
WalletWasabi/Tor/TorProcessManager.cs
@@ -43,7 +43,7 @@ namespace WalletWasabi.Tor
{
TorSocks5EndPoint = torSocks5EndPoint;
_monitorState = StateNotStarted;
- Stop = new CancellationTokenSource();
+ MonitorCts = new CancellationTokenSource();
TorProcess = null;
Settings = settings;
TorSocks5Client = new TorSocks5Client(torSocks5EndPoint);
@@ -63,7 +63,7 @@ namespace WalletWasabi.Tor
public bool IsRunning => Interlocked.Read(ref _monitorState) == StateRunning;
- private CancellationTokenSource Stop { get; set; }
+ private CancellationTokenSource MonitorCts { get; set; }
/// <summary>
/// Installs Tor if it is not installed, then it starts Tor.
@@ -172,7 +172,7 @@ namespace WalletWasabi.Tor
{
try
{
- await Task.Delay(torMisbehaviorCheckPeriod, Stop.Token).ConfigureAwait(false);
+ await Task.Delay(torMisbehaviorCheckPeriod, MonitorCts.Token).ConfigureAwait(false);
if (TorHttpClient.TorDoesntWorkSince is { }) // If Tor misbehaves.
{
@@ -188,7 +188,7 @@ namespace WalletWasabi.Tor
using (var client = new TorHttpClient(baseUri, TorSocks5EndPoint))
{
var message = new HttpRequestMessage(HttpMethod.Get, fallBackTestRequestUri);
- await client.SendAsync(message, Stop.Token).ConfigureAwait(false);
+ await client.SendAsync(message, MonitorCts.Token).ConfigureAwait(false);
}
// Check if it changed in the meantime...
@@ -224,8 +224,14 @@ namespace WalletWasabi.Tor
});
}
+ /// <summary>
+ /// Stops Tor monitor, TCP connection with Tor and Tor process (if it was started).
+ /// </summary>
+ /// <param name="killTor">Whether to kill Tor process or whether it should continue running for privacy reasons.</param>
public async Task StopAsync(bool killTor = false)
{
+ Logger.LogTrace($"> {nameof(killTor)}={killTor}");
+
Interlocked.CompareExchange(ref _monitorState, StateStopping, StateRunning); // If running, make it stopping.
if (TorSocks5EndPoint is null)
@@ -233,14 +239,20 @@ namespace WalletWasabi.Tor
Interlocked.Exchange(ref _monitorState, StateStopped);
}
- Stop?.Cancel();
+ MonitorCts.Cancel();
+
while (Interlocked.CompareExchange(ref _monitorState, StateStopped, StateNotStarted) == StateStopping)
{
await Task.Delay(50).ConfigureAwait(false);
}
- Stop?.Dispose();
- Stop = null;
+ // Stop Tor monitor.
+ MonitorCts.Dispose();
+
+ // Stop TCP connection with Tor.
+ TorSocks5Client.Dispose();
+
+ // Stop Tor itself, if the option is selected by the user.
if (TorProcess is { } && killTor)
{
Logger.LogInfo($"Killing Tor process.");
@@ -259,8 +271,10 @@ namespace WalletWasabi.Tor
}
}
+ // Dispose Tor process resources (does not stop/kill Tor process).
TorProcess?.Dispose();
- TorProcess = null;
+
+ Logger.LogTrace("<");
}
#endregion Monitor

TorProcessManager: Improve documentation, rename `Stop` to `MonitorCts`, add dispose for `TorSocks5Client`.