From 2127e6fa45cc191d0c2de363eb7c7673f8c8a476 Mon Sep 17 00:00:00 2001 From: Daniel Gerlag Date: Sat, 8 Aug 2020 08:44:05 -0700 Subject: [PATCH] shutdown concurrency issue --- .../Services/BackgroundTasks/QueueConsumer.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/WorkflowCore/Services/BackgroundTasks/QueueConsumer.cs b/src/WorkflowCore/Services/BackgroundTasks/QueueConsumer.cs index 54d384879..0d1a5474a 100644 --- a/src/WorkflowCore/Services/BackgroundTasks/QueueConsumer.cs +++ b/src/WorkflowCore/Services/BackgroundTasks/QueueConsumer.cs @@ -61,7 +61,12 @@ private async void Execute() { try { - if (activeTasks.Count >= MaxConcurrentItems) + var activeCount = 0; + lock (activeTasks) + { + activeCount = activeTasks.Count; + } + if (activeCount >= MaxConcurrentItems) { await Task.Delay(Options.IdleTime); continue; @@ -75,14 +80,19 @@ private async void Execute() await Task.Delay(Options.IdleTime, cancelToken); continue; } - - if (activeTasks.ContainsKey(item)) + + var hasTask = false; + lock (activeTasks) + { + hasTask = activeTasks.ContainsKey(item); + } + if (hasTask) { secondPasses.Add(item); if (!EnableSecondPasses) await QueueProvider.QueueWork(item, Queue); continue; - } + } secondPasses.TryRemove(item); @@ -121,7 +131,13 @@ private async void Execute() } } - foreach (var task in activeTasks.Values) + List toComplete; + lock (activeTasks) + { + toComplete = activeTasks.Values.ToList(); + } + + foreach (var task in toComplete) task.Wait(); }