From 34da464bab734f81a7a7209ab5029767c73e800d Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 12 Jun 2021 12:18:55 -0700 Subject: [PATCH] Intercept console interrupt and handle nicely Long ago in the early days of TShock someone asked why CTRL + C wasn't handled and there was an explanation given along the lines of "something something not supported on mono something something" or similar. Attempts were made to try to handle console interrupts unsuccessfully and the code was ripped out. However, it's 2021, and we can now handle this signal and do the right thing (which, ostensibly, is to save the world and shut down). Many people like me reflexively hit CTRL + C because they want to shut down the process. It's very infuriating that the current behavior results in the server just dying and nothing being cleaned up properly. Therefore, this commit changes the behavior to handle the interrupt, save the world, and shut down nicely. (If you still want to shutdown without saving the world, use off-nosave, or idk, send SIGKILL). --- CHANGELOG.md | 1 + TShockAPI/TShock.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1192799c..d1d6ac53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Added `/slay` as an alias for `/kill` to be more consistent with other server mods. (@hakusaro) * Added `/god` as an alias for `/godmode` to be more consistent with other server mods. (@hakusaro) * Fixed ridiculous typo in `Amethyst Gemtree` text. (@hakusaro) +* Fixed `CTRL + C` / interactive console interrupt not safely shutting down the server. Now, interrupts will cause a safe shutdown (saving the world and disconnecting all players before fully shutting down). Previously, interrupts caused an unsafe shutdown (not saving the world). (@hakusaro) ## TShock 4.5.4 * Fixed ridiculous typo in `GetDataHandlers` which caused TShock to read the wrong field in the packet for `usingBiomeTorches`. (@hakusaro, @Arthri) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index c2bfbc39..ed74b882 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -214,6 +214,8 @@ namespace TShockAPI TerrariaApi.Reporting.CrashReporter.HeapshotRequesting += CrashReporter_HeapshotRequesting; + Console.CancelKeyPress += new ConsoleCancelEventHandler(ConsoleCancelHandler); + try { CliParser.Reset(); @@ -638,6 +640,20 @@ namespace TShockAPI } } + /// ConsoleCancelHandler - Handles when Ctrl + C is sent to the server for a safe shutdown. + /// The sender + /// The ConsoleCancelEventArgs associated with the event. + private void ConsoleCancelHandler(object sender, ConsoleCancelEventArgs args) + { + // Cancel the default behavior + args.Cancel = true; + + Log.ConsoleInfo("Interrupt received. Saving the world and shutting down."); + + // Perform a safe shutdown + TShock.Utils.StopServer(true, "Server console interrupted!"); + } + /// HandleCommandLine - Handles the command line parameters passed to the server. /// parms - The array of arguments passed in through the command line. private void HandleCommandLine(string[] parms)