Avoid unhandled exception handlers logging into disposed sawmills on shutdown

This caused a crash & exception swallow on Salamander.
This commit is contained in:
Pieter-Jan Briers
2024-11-11 16:26:04 +01:00
parent 4e100d96bc
commit 21c3535486

View File

@@ -126,13 +126,29 @@ namespace Robust.Server
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