我写了一个简单的基于异步的负载测试库,它还有一个控制台界面可以从命令行进行测试。
基本上,它同时运行大量请求,聚合它们,并显示摘要和简单的直方图。没有什么花哨。但是我在本地系统中运行了很多测试,所以我想确保测试工具能够使用尽可能少的资源来获得相对准确的基准测试。因此它使用带有 Begin/End 方法的纯异步来保持最少的开销。
全部完成,完全异步,它可以工作,并且不碍事(好吧,大部分)。但是正常 session 中的线程数远远超过 40。因此,对于具有 4 个硬件线程的机器来说,这是一种非常巧妙的资源浪费,考虑到本地机器也在运行正在测试的服务器。
我已经在 AsyncContext 中运行该程序,它基本上只是一个简单的排队上下文,将所有内容都放在同一个线程上。所以,所有 aync 回发都在主线程上。完美。
现在,我所要做的就是限制 ThreadPool 的最大线程数,然后看看它的性能如何。将其限制为实际核心,具有 4 个工作线程和 4 个 IOCP 线程。
Result?
Exception: "There were not enough free threads in the ThreadPool to complete the operation."
好吧,这不是一个新问题,并且在整个 Internet 上相当分散。但是,ThreadPool 的全部意义不在于您可以将事情放到池的队列中,并且只要有线程可用它就会执行吗?
事实上,该方法的名称是“Queue”UserWorkItem。并且文档恰本地说,“排队执行一个方法。当线程池线程可用时执行该方法。”
现在,如果没有足够的空闲线程可用,理想情况下,可能会减慢程序的执行速度。 IOCP 和异步任务应该只是排队,但为什么它以这样一种方式实现,它撞倒了,反而失败了?增加线程数不是解决方案,因为它被称为 ThreadPool 旨在成为队列。
Edit - Clarification:
I'm fully aware of the concept of the threadpool, and why the CLR spins up more threads. It should. I agree that it is infact the correct thing to do when there are heavy IO-bound tasks. But the point is, if you do infact restrict the threads in the ThreadPool, it is expected to queue the task for execution whenever a free thread is available, not throw an exception. The concurrency could be affected, perhaps even slowing down the outcome, but a QueueWorkUserItem is intented to Queue, not work only when a new thread is available, or fail - hence, my speculative assertion that its an implementation bug, as stated in the title.
更新 1:
与 Microsoft 支持论坛中记录的相同问题以及示例: http://support.microsoft.com/default.aspx?scid=kb;EN-US;815637
建议的解决方法,显然是增加线程数,因为它无法排队。
注意:这是在非常旧的运行时下,下面给出了在 4.5.1 运行时上重现相同问题的方法。
更新 2:
在 Mono Runtime 上运行相同的代码片段,ThreadPool 似乎没有问题。它被排队,然后被执行。该问题仅在 Microsoft CLR 下发生。
更新 3:
@Noseratio 指出无法在 .NET 4.5.1 下重现相同代码的有效问题后,下面是一段可以重现该问题的代码。为了打破在按预期排队时工作的代码,真正需要做的就是向排队的委托(delegate)添加一个真正的异步调用。
例如,仅将以下行添加到委托(delegate)的末尾应该以异常结束:
(await WebRequest.Create("http://www.google.com").GetResponseAsync()).Close();
复制代码:
这是对 MSKB 文章稍作修改的代码,在 Windows 8.1 的 .NET 4.5.1 下应该会很快失败。
(请随意更改 url 和线程限制)。
public static void Main()
{
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(2, 2);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Queued {0}", i);
ThreadPool.QueueUserWorkItem(PoolFunc);
}
Console.ReadLine();
}
private static async void PoolFunc(object state)
{
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine(
"Available: WorkerThreads: {0}, CompletionPortThreads: {1}",
workerThreads,
completionPortThreads);
Thread.Sleep(1000);
string url = "http://localhost:8080";
HttpWebRequest myHttpWebRequest;
// Creates an HttpWebRequest for the specified URL.
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest, and waits for a response.
Console.WriteLine("Wait for response.");
var myHttpWebResponse = await myHttpWebRequest.GetResponseAsync();
Console.WriteLine("Done.");
myHttpWebResponse.Close();
}
非常感谢任何对此行为的深入了解,这可能会为此提供推理。谢谢。
最佳答案
在您的示例代码中,不是调用 QueueUserWorkItem
引发异常,而是调用 await myHttpWebRequest.GetResponseAsync()
引发异常。如果您查看异常详细信息,您可以准确地看到是什么方法抛出了这个异常
System.InvalidOperationException was unhandled by user code
_HResult=-2146233079
_message=There were not enough free threads in the ThreadPool to complete the operation.
HResult=-2146233079
IsTransient=false
Message=There were not enough free threads in the ThreadPool to complete the operation.
Source=System
StackTrace:
at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory`1.FromAsync(Func`3 beginMethod, Func`2 endMethod, Object state)
at System.Net.WebRequest.<GetResponseAsync>b__8()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ConsoleApplication1.Program.<PoolFunc>d__0.MoveNext() in c:\Users\Justin\Source\Repos\Azure\ConsoleApplication1\ConsoleApplication1\Program.cs:line 39
InnerException:
确实,如果我们看一下 HttpWebRequest.BeginGetResponse
method我们可以看到以下内容
if (!RequestSubmitted && NclUtilities.IsThreadPoolLow())
{
// prevent new requests when low on resources
Exception exception = new InvalidOperationException(SR.GetString(SR.net_needmorethreads));
Abort(exception, AbortState.Public);
throw exception;
}
这个故事的寓意是线程池是其他代码(包括部分 .Net 框架)也使用的共享资源 - 将最大线程数设置为 2 是 Raymond Chen 所说的全局解决方案一个局部问题,结果打破了系统其他部分的预期。
如果您想明确控制正在使用的线程,那么您应该创建自己的实现,但是除非您真的知道自己在做什么,否则最好让 .Net 框架处理线程管理。
关于c# - .NET CLR 线程池耗尽 - 实现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189090/