TorProcessManager: Add `EnsureRunningAsync` method.
MODIFY
WalletWasabi/Tor/TorProcessManager.cs
WalletWasabi/Tor/TorProcessManager.cs
+using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.IO;
@@ -84,7 +85,34 @@ namespace WalletWasabi.Tor
Logger.LogInfo($"Starting Tor process ...");
TorProcess.Start();
- // Ensure it's running.
+ bool isRunning = await EnsureRunningAsync(TorProcess, token).ConfigureAwait(false);
+
+ if (!isRunning)
+ {
+ return false;
+ }
+
+ Logger.LogInfo("Tor is running.");
+ TorControlClient = await InitTorControlAsync(token).ConfigureAwait(false);
+
+ return true;
+ }
+ catch (OperationCanceledException ex)
+ {
+ Logger.LogDebug("User canceled operation.", ex);
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Logger.LogError("Could not automatically start Tor. Try running Tor manually.", ex);
+ }
+
+ return false;
+ }
+
+ /// <summary>Ensure <paramref name="process"/> is actually running.</summary>
+ private async Task<bool> EnsureRunningAsync(ProcessAsync process, CancellationToken token)
+ {
int i = 0;
while (true)
{
@@ -94,10 +122,10 @@ namespace WalletWasabi.Tor
if (isRunning)
{
- break;
+ return true;
}
- if (TorProcess.HasExited)
+ if (process.HasExited)
{
Logger.LogError("Tor process failed to start!");
return false;
@@ -114,23 +142,6 @@ namespace WalletWasabi.Tor
// Wait 250 milliseconds between attempts.
await Task.Delay(250, token).ConfigureAwait(false);
}
-
- Logger.LogInfo("Tor is running.");
- TorControlClient = await InitTorControlAsync(token).ConfigureAwait(false);
-
- return true;
- }
- catch (OperationCanceledException ex)
- {
- Logger.LogDebug("User canceled operation.", ex);
- throw;
- }
- catch (Exception ex)
- {
- Logger.LogError("Could not automatically start Tor. Try running Tor manually.", ex);
- }
-
- return false;
}
/// <summary>

TorProcessManager: Add `EnsureRunningAsync` method.