diff --git a/Projects/Server/Console/ConsoleInputHandler.cs b/Projects/Server/Console/ConsoleInputHandler.cs index 6891dc83b..3c621085b 100644 --- a/Projects/Server/Console/ConsoleInputHandler.cs +++ b/Projects/Server/Console/ConsoleInputHandler.cs @@ -152,30 +152,38 @@ public static class ConsoleInputHandler private static async void ProcessConsoleInput() { - var token = Core.ClosingTokenSource.Token; - - while (!token.IsCancellationRequested) + while (!Core.Closing) { + string input; try { - var input = Console.ReadLine()?.Trim(); + input = Console.ReadLine()?.Trim(); + } + catch + { + logger.Warning("Console commands have been disabled due to an error."); + _initialized = false; + return; + } - if (Volatile.Read(ref _expectUserInput)) - { - _input = input; - _receivedUserInput.Set(); - _endUserInput.WaitOne(); - continue; - } + if (Volatile.Read(ref _expectUserInput)) + { + _input = input; + _receivedUserInput.Set(); + _endUserInput.WaitOne(); + continue; + } - if (string.IsNullOrEmpty(input)) - { - continue; - } + if (string.IsNullOrEmpty(input)) + { + continue; + } - var splitInput = input.Split(' ', 2); - var command = splitInput[0].ToLower(); + var splitInput = input.Split(' ', 2); + var command = splitInput[0].ToLower(); + try + { Action action; lock (_inputCommands) { @@ -186,7 +194,7 @@ public static class ConsoleInputHandler } catch (Exception e) { - logger.Warning(e, "Failed to process console input"); + logger.Error(e, "Failed to execute console command: {Command}", input); } } } diff --git a/Projects/Server/Main.cs b/Projects/Server/Main.cs index e62a715a2..bacae4518 100644 --- a/Projects/Server/Main.cs +++ b/Projects/Server/Main.cs @@ -304,8 +304,8 @@ public static class Core public static void Kill(bool restart = false) { - _performProcessKill = true; _restartOnKill = restart; + _performProcessKill = true; } public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) @@ -338,7 +338,7 @@ public static class Core ConsoleInputHandler.ReadLine(); } - Kill(); + DoKill(); } } @@ -379,18 +379,16 @@ public static class Core logger.Information("Restarting"); if (IsWindows) { - Process.Start("dotnet", ApplicationAssembly.Location); + using var process = Process.Start("dotnet", $"{ApplicationAssembly.Location} {Arguments}"); } else { - var process = new Process + using var process = new Process(); + process.StartInfo = new ProcessStartInfo { - StartInfo = new ProcessStartInfo - { - FileName = "dotnet", - Arguments = ApplicationAssembly.Location, - UseShellExecute = true - } + FileName = "dotnet", + Arguments = $"{ApplicationAssembly.Location} {Arguments}", + UseShellExecute = true }; process.Start(); @@ -403,7 +401,7 @@ public static class Core } } - Process.Kill(); + Environment.Exit(0); } private static void HandleClosed() @@ -561,7 +559,7 @@ public static class Core if (_performProcessKill) { - DoKill(_restartOnKill); + break; } _tickCount = 0; @@ -590,11 +588,10 @@ public static class Core catch (Exception e) { CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true)); + return; } - finally - { - World.ExitSerializationThreads(); - } + + DoKill(_restartOnKill); } internal static void RequestSnapshot() => _performSnapshot = true;