From 58340e0f5d5e89f2caa8773e00580b0ad5be3e30 Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Sat, 9 Nov 2024 19:05:40 +0700 Subject: [PATCH] fix reoccurring entry dll path in args when application restarts --- Torch.Server/ApplicationRestartHelper.cs | 39 +++++++++++++++++++++++ Torch.Server/TorchServer.cs | 19 +---------- Torch.Server/UnhandledExceptionHandler.cs | 19 +---------- 3 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 Torch.Server/ApplicationRestartHelper.cs diff --git a/Torch.Server/ApplicationRestartHelper.cs b/Torch.Server/ApplicationRestartHelper.cs new file mode 100644 index 0000000..a23070e --- /dev/null +++ b/Torch.Server/ApplicationRestartHelper.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; + +namespace Torch.Server; + +internal static class ApplicationRestartHelper +{ + public static void RestartApplication() + { + var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe"); + + var args = Environment.GetCommandLineArgs().ToList(); + + args.RemoveAt(0); // dll entry path + + args.RemoveConfigItem("--tempAutostart"); + args.RemoveConfigItem("--waitForPid"); + + args.AddRange([ + "--waitForPid", Environment.ProcessId.ToString(), + "--tempAutostart", "true" + ]); + + Process.Start(new ProcessStartInfo(exe, args)); + } + + private static void RemoveConfigItem(this List list, string item) + { + var index = list.BinarySearch(item, StringComparer.OrdinalIgnoreCase); + + if (index < 0) return; + + list.RemoveAt(index); + list.RemoveAt(index); + } +} \ No newline at end of file diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index fb24a21..2fb9c90 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -269,24 +269,7 @@ namespace Torch.Server ) return; - var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe"); - - var args = Environment.GetCommandLineArgs(); - - for (var i = 0; i < args.Length; i++) - { - if (args[i].Contains(' ')) - args[i] = $"\"{args[i]}\""; - - if (!args[i].Contains("--tempAutostart", StringComparison.InvariantCultureIgnoreCase) && - !args[i].Contains("--waitForPid", StringComparison.InvariantCultureIgnoreCase)) - continue; - - args[i] = string.Empty; - args[++i] = string.Empty; - } - - Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}"); + ApplicationRestartHelper.RestartApplication(); }) { Name = "Restart thread" diff --git a/Torch.Server/UnhandledExceptionHandler.cs b/Torch.Server/UnhandledExceptionHandler.cs index 2b05d05..486cfde 100644 --- a/Torch.Server/UnhandledExceptionHandler.cs +++ b/Torch.Server/UnhandledExceptionHandler.cs @@ -33,24 +33,7 @@ internal class UnhandledExceptionHandler Console.WriteLine("Restarting in 5 seconds."); Thread.Sleep(5000); - var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe"); - - var args = Environment.GetCommandLineArgs(); - - for (var i = 0; i < args.Length; i++) - { - if (args[i].Contains(' ')) - args[i] = $"\"{args[i]}\""; - - if (!args[i].Contains("--tempAutostart", StringComparison.InvariantCultureIgnoreCase) && - !args[i].Contains("--waitForPid", StringComparison.InvariantCultureIgnoreCase)) - continue; - - args[i] = string.Empty; - args[++i] = string.Empty; - } - - Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}"); + ApplicationRestartHelper.RestartApplication(); } else {