问题
My 64bit application uses Tasks a lot and now I appear to have a situation in which Task.Run
isn't executed. The ThreadPool has 32768 threads AFAIK and I cannot imagine that all of these are used.
What could prevent Task.Run
from executing? And how can I diagnose the problem?
UPDATE
Before reading Marc's suggestion below, we ended up wrapping all Task.Run calls in a simple, static class that allowed us to log and trace what was happening.
Something like this:
public static Task Run(Action action, [CallerMemberName] string name = null)
{
var stat = _stats.GetOrAdd(name, n => new TaskStats());
stat.IncrementStarted();
var task = Task.Run(() =>
{
stat.IncrementExecuted();
action();
});
task.ContinueWith(t => stat.IncrementCompleted(t));
return task;
}
After doing so, we indeed discovered that tasks were not being executed, i.e. IncrementStarted was called more often than IncrementExecuted. Due to the context information (sometimes we replaced CallerMemberName with more instance-specific details), we discovered that we were creating many, many Tasks in a rather tight loop (~100 Tasks/s) that were not being executed.
These were fire-and-forget tasks that we replaced with a custom queue implementation (around a BlockingCollection
) and as of now it appears to have solved the problem. Although: all of this only happened on one specific production machine and we haven't yet found out why, i.e. what makes this machine so "special".
Thanks to our logging we can still observe (on this one specific machine) some unexpected/sub-optimal task-queueing issues that we will continue to look into.
Thanks @MarcGravell, his story about how he took down StackOverflow pushed us in the right direction. Basically starting from his link we came across the following sites that were very interesting and helpful:
https://blog.marcgravell.com/2019/02/fun-with-spiral-of-death.html?m=1 https://labs.criteo.com/2018/10/net-threadpool-starvation-and-how-queuing-makes-it-worse/ http://www.danielmoth.com/Blog/New-And-Improved-CLR-4-Thread-Pool-Engine.aspx https://devblogs.microsoft.com/premier-developer/the-danger-of-taskcompletionsourcet-class/
来源:https://stackoverflow.com/questions/61096543/task-run-doesnt-execute