From 5c35a3be45cd15a131c82882c481d0218f1f1075 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Wed, 15 Oct 2025 14:03:17 -0500 Subject: [PATCH] fix: catch exceptions in handlers directly Previously, any exceptions that were occuring in a hander's action were being swalled due to being off on another thread. Now, those exceptions are caught and printed out. --- .../MobileControlSystemController.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/PepperDash.Essentials.MobileControl/MobileControlSystemController.cs b/src/PepperDash.Essentials.MobileControl/MobileControlSystemController.cs index 1012abe8..01bf3579 100644 --- a/src/PepperDash.Essentials.MobileControl/MobileControlSystemController.cs +++ b/src/PepperDash.Essentials.MobileControl/MobileControlSystemController.cs @@ -2337,10 +2337,33 @@ namespace PepperDash.Essentials foreach (var handler in handlers) { - Task.Run( - () => - handler.Action(message.Type, message.ClientId, message.Content) - ); + Task.Run(async () => + { + try + { + handler.Action(message.Type, message.ClientId, message.Content); + } + catch (Exception ex) + { + this.LogError( + "Exception in handler for message type {type}, ClientId {clientId}", + message.Type, + message.ClientId + ); + this.LogDebug(ex, "Stack Trace: "); + } + }).ContinueWith(task => + { + if (task.IsFaulted && task.Exception != null) + { + this.LogError( + "Unhandled exception in Task for message type {type}, ClientId {clientId}", + message.Type, + message.ClientId + ); + this.LogDebug(task.Exception.GetBaseException(), "Stack Trace: "); + } + }, TaskContinuationOptions.OnlyOnFaulted); } break;