Use async API for TorProcessManager.Start().
MODIFY
WalletWasabi.Gui/Global.cs
WalletWasabi.Gui/Global.cs
@@ -176,7 +176,7 @@ namespace WalletWasabi.Gui
if (Config.UseTor)
{
TorManager = new TorProcessManager(TorSettings, Config.TorSocks5EndPoint);
- TorManager.Start(ensureRunning: false);
+ await TorManager.StartAsync(ensureRunning: false).ConfigureAwait(false);
var fallbackRequestTestUri = new Uri(Config.GetFallbackBackendUri(), "/api/software/versions");
TorManager.StartMonitor(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(7), fallbackRequestTestUri);

MODIFY
WalletWasabi.Tests/IntegrationTests/LiveServerTests.cs
WalletWasabi.Tests/IntegrationTests/LiveServerTests.cs
@@ -28,8 +28,7 @@ namespace WalletWasabi.Tests.IntegrationTests
public async Task InitializeAsync()
{
var torManager = new TorProcessManager(Global.Instance.TorSettings, Global.Instance.TorSocks5Endpoint);
- torManager.Start(ensureRunning: true);
- await Task.Delay(3000);
+ await torManager.StartAsync(ensureRunning: true);
}
public Task DisposeAsync()

MODIFY
WalletWasabi.Tests/IntegrationTests/TorTests.cs
WalletWasabi.Tests/IntegrationTests/TorTests.cs
@@ -17,8 +17,7 @@ namespace WalletWasabi.Tests.IntegrationTests
public async Task InitializeAsync()
{
var torManager = new TorProcessManager(Global.Instance.TorSettings, Global.Instance.TorSocks5Endpoint);
- torManager.Start(ensureRunning: true);
- await Task.Delay(3000);
+ await torManager.StartAsync(ensureRunning: true);
}
public Task DisposeAsync()

MODIFY
WalletWasabi/Tor/TorProcessManager.cs
WalletWasabi/Tor/TorProcessManager.cs
@@ -60,26 +60,29 @@ namespace WalletWasabi.Tor
private CancellationTokenSource Stop { get; set; }
- public void Start(bool ensureRunning)
- {
- new Thread(delegate () // Do not ask. This is the only way it worked on Win10/Ubuntu18.04/Manjuro(1 processor VM)/Fedora(1 processor VM)
+ public async Task StartAsync(bool ensureRunning)
{
try
{
- // 1. Is it already running?
- // 2. Can I simply run it from output directory?
- // 3. Can I copy and unzip it from assets?
- // 4. Throw exception.
-
try
{
- if (IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
+ // Is Tor already running? Either our Tor process from previous Wasabi Wallet run or possibly user's own Tor.
+ bool isAlreadyRunning = await IsTorRunningAsync(TorSocks5EndPoint).ConfigureAwait(false);
+
+ if (isAlreadyRunning)
{
Logger.LogInfo("Tor is already running.");
return;
}
- new TorInstallator(Settings).VerifyInstallationAsync().GetAwaiter().GetResult();
+ // Install Tor if it is not installed and verify Tor is not tampered with (using hash/checksum).
+ bool verified = await new TorInstallator(Settings).VerifyInstallationAsync().ConfigureAwait(false);
+
+ if (!verified)
+ {
+ Logger.LogInfo("Failed to verify Tor instalation.");
+ return;
+ }
string torArguments = Settings.GetCmdArguments(TorSocks5EndPoint);
@@ -118,11 +121,28 @@ namespace WalletWasabi.Tor
if (ensureRunning)
{
- Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult(); // dotnet brainfart, ConfigureAwait(false) IS NEEDED HERE otherwise (only on) Manjuro Linux fails, WTF?!!
- if (!IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
+ int i = 0;
+ while (true)
+ {
+ i++;
+
+ bool isRunning = await IsTorRunningAsync(TorSocks5EndPoint).ConfigureAwait(false);
+
+ if (isRunning)
+ {
+ break;
+ }
+
+ // At most 10 attempts.
+ if (i >= 10)
{
throw new TorException("Attempted to start Tor, but it is not running.");
}
+
+ // Wait 1500 milliseconds between attempts.
+ await Task.Delay(1500).ConfigureAwait(false);
+ }
+
Logger.LogInfo("Tor is running.");
}
}
@@ -135,7 +155,6 @@ namespace WalletWasabi.Tor
{
Logger.LogError(ex);
}
- }).Start();
}
/// <param name="torSocks5EndPoint">Opt out Tor with null.</param>
@@ -202,8 +221,7 @@ namespace WalletWasabi.Tor
else
{
Logger.LogInfo($"Tor did not work properly for {(int)torMisbehavedFor.TotalSeconds} seconds. Maybe it crashed. Attempting to start it...");
- Start(ensureRunning: true); // Try starting Tor, if it does not work it'll be another issue.
- await Task.Delay(14000, Stop.Token).ConfigureAwait(false);
+ await StartAsync(ensureRunning: true).ConfigureAwait(false); // Try starting Tor, if it does not work it'll be another issue.
}
}
}

Use async API for TorProcessManager.Start().