From 1c636733f5552adc4956b6c01aba3dcef1544896 Mon Sep 17 00:00:00 2001 From: Ian Cooper Date: Tue, 31 Dec 2024 18:32:00 +0000 Subject: [PATCH] fix: missing code, should have been in last commit --- .../Tasks/BrighterTaskScheduler.cs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Paramore.Brighter/Tasks/BrighterTaskScheduler.cs b/src/Paramore.Brighter/Tasks/BrighterTaskScheduler.cs index 58ae28df90..539b2c45ae 100644 --- a/src/Paramore.Brighter/Tasks/BrighterTaskScheduler.cs +++ b/src/Paramore.Brighter/Tasks/BrighterTaskScheduler.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Threading; using System.Threading.Tasks; namespace Paramore.Brighter.Tasks; @@ -60,13 +61,14 @@ protected override void QueueTask(Task task) var queued = _synchronizationHelper.Enqueue((Task)task, false); if (!queued) - { + { Debug.IndentLevel = 1; Debug.WriteLine($"BrighterTaskScheduler: QueueTask Failed to queue task {task.ToString()} on {System.Threading.Thread.CurrentThread.ManagedThreadId}"); Debug.IndentLevel = 0; + new Thread(TryExecuteNewThread) { IsBackground = true }.Start(task); } } - + /// /// Attempts to execute the specified task on the current thread. /// @@ -103,4 +105,28 @@ public void DoTryExecuteTask(Task task) TryExecuteTask(task); } + + /// + /// In a new thread, attempts to execute the specified task. + /// + /// + /// This is a little "Hail Mary" to try and execute a task that has failed to queue because we have already completed the synchronization context. + /// Seems to be caused by an Exection Context that has our scheduler as the default, as it fools ConfigureAwait + /// + /// + /// + private void TryExecuteNewThread(object? obj) + { + var task = obj as Task; + if (task == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + Debug.IndentLevel = 1; + Debug.WriteLine($"BrighterTaskScheduler: Use TryExecuteNewThread for {task} on thread {System.Threading.Thread.CurrentThread.ManagedThreadId}"); + Debug.IndentLevel = 0; + + TryExecuteTask(task); + } }