I'm running in a race condition rather difficult to reproduce, and from my code analysis it seems to come from a continuation that doesn't execute (or not until its end).
Here is some pseudo-code presenting the context:
Task<Something> GetObject(string id)
{
// some async code retrieving the object
}
List<Task> tasks = new List<Task>();
List<Something> result = new List<Something>();
foreach (var id in someList)
{
tasks.Add(GetObject(id).ContinueWith(task =>
{
var something = task.Result;
// do some basic sync stuff on something
result.Add(something);
}));
}
await Task.WhenAll(tasks);
It randomly happens that the result is missing some of the expected objects.
For some reason, I know that the foreach
went well and there is no reason why all the required tasks would not have been added to the List<Task>
.
Instrumenting or debugging hasn't given results so far as it seems to improve the race condition... I hope someone can shed some lights here. Thanks!