diff --git a/AsyncScheduler/Scheduler.cs b/AsyncScheduler/Scheduler.cs index 5672f68..7da30fb 100644 --- a/AsyncScheduler/Scheduler.cs +++ b/AsyncScheduler/Scheduler.cs @@ -388,7 +388,17 @@ private async void ResolveTaskEnd(string jobKey, Task task) } finally { - _runningJobs.TryRemove(jobKey, out _); + bool removed; + do + { + removed = _runningJobs.TryRemove(jobKey, out _); + if (!removed) + { + _logger.LogWarning("Unable to remove running job from ConcurrentDictionary: {jobKey} ... Retry...", jobKey); + await Task.Delay(TimeSpan.FromMilliseconds(50)); + } + } while (removed == false); + if (task is IDisposable disposable) { disposable.Dispose();