From e9d4c3eb641388c1b57731ac7e518cd816877a5b Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 10 Apr 2026 11:22:40 +0300 Subject: [PATCH] fix: coroutine managers now handle exceptions --- Engine.Core/Systems/CoroutineManager.cs | 18 +++++++-- Engine.Core/Systems/NestedCoroutineManager.cs | 40 ++++++++++++------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/Engine.Core/Systems/CoroutineManager.cs b/Engine.Core/Systems/CoroutineManager.cs index 16f9390..4c6be18 100644 --- a/Engine.Core/Systems/CoroutineManager.cs +++ b/Engine.Core/Systems/CoroutineManager.cs @@ -1,6 +1,8 @@ using System.Collections; using System.Collections.Generic; +using Engine.Core.Debug; + namespace Engine.Core; public class CoroutineManager : Behaviour, IUpdate @@ -22,11 +24,21 @@ public class CoroutineManager : Behaviour, IUpdate { for (int i = enumerators.Count - 1; i >= 0; i--) { - if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield()) - continue; + try + { + if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield()) + continue; - if (!enumerators[i].MoveNext()) + if (!enumerators[i].MoveNext()) + enumerators.RemoveAt(i); + } + catch (System.Exception exception) + { + ILogger.Shared.LogError(this, $"Coroutine failed, removing from execution."); + ILogger.Shared.LogException(this, exception); + ILogger.Shared.LogTrace(exception.StackTrace); enumerators.RemoveAt(i); + } } } diff --git a/Engine.Core/Systems/NestedCoroutineManager.cs b/Engine.Core/Systems/NestedCoroutineManager.cs index e53bad2..9ee784e 100644 --- a/Engine.Core/Systems/NestedCoroutineManager.cs +++ b/Engine.Core/Systems/NestedCoroutineManager.cs @@ -1,6 +1,8 @@ using System.Collections; using System.Collections.Generic; +using Engine.Core.Debug; + namespace Engine.Core; public class NestedCoroutineManager : Behaviour, IUpdate, ICoroutineManager @@ -47,23 +49,33 @@ public class NestedCoroutineManager : Behaviour, IUpdate, ICoroutineManager continue; } - IEnumerator top = stack.Peek(); - - if (top.Current is ICoroutineYield coroutineYield && coroutineYield.Yield()) - continue; - - if (top.Current is IEnumerator nested) + try { - stack.Push(nested); - continue; + IEnumerator top = stack.Peek(); + + if (top.Current is ICoroutineYield coroutineYield && coroutineYield.Yield()) + continue; + + if (top.Current is IEnumerator nested) + { + stack.Push(nested); + continue; + } + + if (!top.MoveNext()) + { + stack.Pop(); + if (stack.Count != 0) + stack.Peek().MoveNext(); + continue; + } } - - if (!top.MoveNext()) + catch (System.Exception exception) { - stack.Pop(); - if (stack.Count != 0) - stack.Peek().MoveNext(); - continue; + ILogger.Shared.LogError(this, $"Coroutine failed, removing from execution."); + ILogger.Shared.LogException(this, exception); + ILogger.Shared.LogTrace(exception.StackTrace); + RemoveCoroutineAt(i); } } }