From 83dfc7152fc078d5d1f2fa6e7f206177364ff94c Mon Sep 17 00:00:00 2001
From: zznty <94796179+zznty@users.noreply.github.com>
Date: Thu, 13 Jul 2023 13:18:20 +0700
Subject: [PATCH] properly handle sigterm and dont freeze forever on unload
---
Torch.Server/TorchServer.cs | 15 ++++++++++++++-
Torch.Server/Views/TorchUI.xaml.cs | 2 +-
Torch/TorchBase.cs | 4 ++--
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs
index b0e7a9e..0f0e83e 100644
--- a/Torch.Server/TorchServer.cs
+++ b/Torch.Server/TorchServer.cs
@@ -69,6 +69,14 @@ namespace Torch.Server
_simUpdateTimer.Elapsed += SimUpdateElapsed;
_simUpdateTimer.Start();
+
+ Console.CancelKeyPress += (_, _) =>
+ {
+ if (State == ServerState.Running)
+ Stop();
+
+ Environment.Exit(0);
+ };
}
private void SimUpdateElapsed(object sender, System.Timers.ElapsedEventArgs e)
@@ -239,9 +247,14 @@ namespace Torch.Server
StopInternal();
LogManager.Flush();
+ if (
#if DEBUG
- Environment.Exit(0);
+ // ReSharper disable once ConditionIsAlwaysTrueOrFalse
+ true ||
#endif
+ ApplicationContext.Current.IsService
+ )
+ Environment.Exit(0);
var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
diff --git a/Torch.Server/Views/TorchUI.xaml.cs b/Torch.Server/Views/TorchUI.xaml.cs
index 7bbefd8..aa8542a 100644
--- a/Torch.Server/Views/TorchUI.xaml.cs
+++ b/Torch.Server/Views/TorchUI.xaml.cs
@@ -116,7 +116,7 @@ namespace Torch.Server
if (_server?.State == ServerState.Running)
_server.Stop();
- Process.GetCurrentProcess().Kill();
+ Environment.Exit(0);
}
}
}
diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs
index 932d70c..400c3c7 100644
--- a/Torch/TorchBase.cs
+++ b/Torch/TorchBase.cs
@@ -372,10 +372,10 @@ namespace Torch
///
public virtual void Stop()
{
- LogManager.Flush();
Game.SignalStop();
- if (!Game.WaitFor(VRageGame.GameState.Stopped))
+ if (!Game.WaitFor(VRageGame.GameState.Stopped, TimeSpan.FromSeconds(Config.TickTimeout)))
Log.Warn("Failed to wait for the game to be stopped");
+ LogManager.Flush();
}
///