From 07ee6ce5860be73f9633cb3cb5a2bcd1d6962c30 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 4 Jun 2020 14:11:48 -0400 Subject: [PATCH] fix: Deadlock detection works better for false positives --- .../SafeCriticalSection.SimplSharp.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils/SafeCriticalSection.SimplSharp.cs b/ICD.Common.Utils/SafeCriticalSection.SimplSharp.cs index 6aa40f1..dae97c1 100644 --- a/ICD.Common.Utils/SafeCriticalSection.SimplSharp.cs +++ b/ICD.Common.Utils/SafeCriticalSection.SimplSharp.cs @@ -1,4 +1,6 @@ using System; +using ICD.Common.Utils.Services; +using ICD.Common.Utils.Services.Logging; #if SIMPLSHARP using Crestron.SimplSharp; @@ -44,8 +46,20 @@ namespace ICD.Common.Utils try { #if DEBUG - if (!m_CriticalSection.WaitForMutex(TIMEOUT)) - throw new InvalidProgramException("Deadlock detected in program"); + int attempt = 1; + while (!m_CriticalSection.WaitForMutex(TIMEOUT)) + { + // Poor-man's stack trace + try + { + throw new InvalidProgramException("Failed to aquire mutex in expected timeframe - Attempt " + attempt++); + } + catch (Exception e) + { + ServiceProvider.GetService() + .AddEntry(eSeverity.Error, e, "Deadlock detected in program"); + } + } #else m_CriticalSection.Enter(); #endif