From 21c3535486856863d815eee8c9cf902854d208e5 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Mon, 11 Nov 2024 16:26:04 +0100 Subject: [PATCH] Avoid unhandled exception handlers logging into disposed sawmills on shutdown This caused a crash & exception swallow on Salamander. --- Robust.Server/Program.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Robust.Server/Program.cs b/Robust.Server/Program.cs index 52c2067165b..af66f453310 100644 --- a/Robust.Server/Program.cs +++ b/Robust.Server/Program.cs @@ -126,13 +126,29 @@ internal static void SetupLogging(IDependencyCollection deps) AppDomain.CurrentDomain.UnhandledException += (sender, args) => { var message = ((Exception) args.ExceptionObject).ToString(); - uh.Log(args.IsTerminating ? LogLevel.Fatal : LogLevel.Error, message); + try + { + uh.Log(args.IsTerminating ? LogLevel.Fatal : LogLevel.Error, message); + } + catch (ObjectDisposedException) + { + // Avoid eating the exception if it's during shutdown and the sawmill is already gone. + System.Console.WriteLine($"UnhandledException but sawmill is disposed! {message}"); + } }; var uo = mgr.GetSawmill("unobserved"); TaskScheduler.UnobservedTaskException += (sender, args) => { - uo.Error(args.Exception!.ToString()); + try + { + uo.Error(args.Exception!.ToString()); + } + catch (ObjectDisposedException) + { + // Avoid eating the exception if it's during shutdown and the sawmill is already gone. + System.Console.WriteLine($"UnobservedTaskException but sawmill is disposed! {args.Exception}"); + } #if EXCEPTION_TOLERANCE args.SetObserved(); // don't crash #endif