Skip to content

Commit

Permalink
Fix race condition in TaskThrottler (#159)
Browse files Browse the repository at this point in the history
We waited for tasks to complete inside the loop, which meant that if cancelation of the loop was requested while it was in the part of the code after that (for example waiting on the mutex), they would never be awaited.

There is no real reason to wait inside the loop, we can just wait outside it, it is effectively the same.
  • Loading branch information
einarmo authored Jan 14, 2022
1 parent f49c7ad commit 6bf16f0
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions Cognite.Common/TaskThrottler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ private async Task Run()
catch (TaskCanceledException)
{
}


}
else
{
Expand All @@ -338,17 +336,11 @@ await CommonUtils
_taskCompletionEvent.Reset();
}


lock (_lock)
{
if (_quitOnFailure && _runningTasks.Any(result => result.Task != null && result.Task.IsFaulted)) break;
}

if (_source.IsCancellationRequested || _generators.IsCompleted)
{
await Task.WhenAll(_runningTasks.Select(result => result.Task)).ConfigureAwait(false);
}

lock (_lock)
{
var now = DateTime.UtcNow;
Expand All @@ -367,6 +359,10 @@ await CommonUtils
|| result.Task.IsFaulted);
}
}

await Task.WhenAll(
_runningTasks.Select(result => result.Task)
.Where(task => task != null && !task.IsCompleted)).ConfigureAwait(false);
}

/// <summary>
Expand Down

0 comments on commit 6bf16f0

Please sign in to comment.