Only stop nodes that I own.
MODIFY
WalletWasabi/BitcoinCore/CoreNode.cs
WalletWasabi/BitcoinCore/CoreNode.cs
@@ -112,7 +112,7 @@ namespace WalletWasabi.BitcoinCore
coreNode.RpcClient = new RPCClient($"{authString}", coreNode.RpcEndPoint.ToString(coreNode.Network.DefaultPort), coreNode.Network);
- if (coreNodeParams.TryRestart && await coreNode.TryStopAsync().ConfigureAwait(false) && coreNodeParams.TryDeleteDataDir)
+ if (coreNodeParams.TryRestart && await coreNode.TryStopAsync(false).ConfigureAwait(false) && coreNodeParams.TryDeleteDataDir)
{
await IoHelpers.DeleteRecursivelyWithMagicDustAsync(coreNode.DataDir).ConfigureAwait(false);
}
@@ -207,14 +207,15 @@ namespace WalletWasabi.BitcoinCore
return await Task.WhenAll(tasks).ConfigureAwait(false);
}
- public async Task<bool> TryStopAsync()
+ /// <param name="onlyOwned">Only stop if this node owns the process.</param>
+ public async Task<bool> TryStopAsync(bool onlyOwned = true)
{
Exception exThrown = null;
if (Bridge != null)
{
try
{
- await Bridge.StopAsync().ConfigureAwait(false);
+ await Bridge.StopAsync(onlyOwned).ConfigureAwait(false);
Logger.LogInfo("Stopped.");
return true;
}

MODIFY
WalletWasabi/BitcoinCore/Processes/BitcoindRpcProcessBridge.cs
WalletWasabi/BitcoinCore/Processes/BitcoindRpcProcessBridge.cs
@@ -23,6 +23,7 @@ namespace WalletWasabi.BitcoinCore.Processes
public string DataDir { get; }
public bool PrintToConsole { get; }
public PidFile PidFile { get; }
+ private int? CachedPid { get; set; }
public BitcoindRpcProcessBridge(RPCClient rpc, string dataDir, bool printToConsole) : base()
{
@@ -31,6 +32,7 @@ namespace WalletWasabi.BitcoinCore.Processes
DataDir = Guard.NotNullOrEmptyOrWhitespace(nameof(dataDir), dataDir);
PrintToConsole = printToConsole;
PidFile = new PidFile(DataDir, Network);
+ CachedPid = null;
}
public async Task StartAsync()
@@ -39,6 +41,7 @@ namespace WalletWasabi.BitcoinCore.Processes
using var process = Start($"{NetworkTranslator.GetCommandLineArguments(Network)} -datadir={DataDir} -printtoconsole={ptcv}", false);
await PidFile.SerializeAsync(process.Id).ConfigureAwait(false);
+ CachedPid = process.Id;
while (true)
{
@@ -55,21 +58,27 @@ namespace WalletWasabi.BitcoinCore.Processes
}
}
- public async Task StopAsync()
+ /// <param name="onlyOwned">Only stop if this node owns the process.</param>
+ public async Task StopAsync(bool onlyOwned)
{
+ var rpcRan = false;
try
{
var reasonableCoreShutdownTimeout = TimeSpan.FromSeconds(21);
using CancellationTokenSource cts = new CancellationTokenSource(reasonableCoreShutdownTimeout);
- var pid = await PidFile.TryReadAsync().ConfigureAwait(false);
+ int? pid = await PidFile.TryReadAsync().ConfigureAwait(false);
- if (pid.HasValue)
+ // If the cached pid is pid, then we own the process.
+ if (!pid.HasValue && (!onlyOwned || CachedPid == pid))
+ {
+ try
{
using Process process = Process.GetProcessById(pid.Value);
try
{
await RpcClient.StopAsync().ConfigureAwait(false);
+ rpcRan = true;
}
catch (Exception ex)
{
@@ -78,11 +87,19 @@ namespace WalletWasabi.BitcoinCore.Processes
}
await process.WaitForExitAsync(cts.Token).ConfigureAwait(false);
}
- }
finally
{
PidFile.TryDelete();
}
}
}
+ catch
+ {
+ if (!onlyOwned && !rpcRan)
+ {
+ await RpcClient.StopAsync().ConfigureAwait(false);
+ }
+ }
+ }
+ }
}

Only stop nodes that I own.