什么会使PerformanceCounterCategory.Exists无限期挂起?

dri*_*iis 39 .net c# debugging performancecounter

我有一个使用性能计数器的应用程序,已经工作了几个月.现在,在我的开发机器和另一台开发人员机器上,当我调用PerformanceCounterCategory.Exists时它已经开始挂起.据我所知,它无限期地挂起.我使用哪个类别作为输入并不重要,使用API​​的其他应用程序表现出相同的行为.

调试(使用MS Symbol Servers)显示它是对挂起的Microsoft.Win32.RegistryKey的调用.进一步的调查表明,这条线是挂起的:

while (Win32Native.ERROR_MORE_DATA == (r = Win32Native.RegQueryValueEx(hkey, name, null, ref type, blob, ref sizeInput))) { 
Run Code Online (Sandbox Code Playgroud)

这基本上是一个尝试为性能计数器数据分配足够内存的循环.它开始于size = 65000并进行一些迭代.在第4次通话中size = 520000,Win32Native.RegQueryValueEx挂起时.

此外,相当令人担忧的是,我在PerformanceCounterLib.GetData的参考源中找到了这条评论:

    // Win32 RegQueryValueEx for perf data could deadlock (for a Mutex) up to 2mins in some 
    // scenarios before they detect it and exit gracefully. In the mean time, ERROR_BUSY,
    // ERROR_NOT_READY etc can be seen by other concurrent calls (which is the reason for the 
    // wait loop and switch case below). We want to wait most certainly more than a 2min window. 
    // The curent wait time of up to 10mins takes care of the known stress deadlock issues. In most
    // cases we wouldn't wait for more than 2mins anyways but in worst cases how much ever time 
    // we wait may not be sufficient if the Win32 code keeps running into this deadlock again
    // and again. A condition very rare but possible in theory. We would get back to the user
    // in this case with InvalidOperationException after the wait time expires.
Run Code Online (Sandbox Code Playgroud)

以前有人见过这种行为吗?我该怎么做才能解决这个问题?

dri*_*iis 29

这个问题现在已修复,由于此处没有答案,我将在此处添加答案,以防将来的搜索中出现问题.

我最终通过停止打印后台处理程序服务(作为临时措施)来修复此错误.

看起来性能计数器的读取实际上需要枚举系统上的打印机(通过挂起进程的WinDbg转储确认,我可以在堆栈跟踪中看到winspool是枚举打印机,并且卡在网络调用中) .这实际上是在系统上失败的(确实,打开"设备和打印机"窗口也挂起).令我感到困惑的是,打印机/网络问题实际上会使性能计数器下降.人们会认为这种情况下存在某种故障安全措施.

我猜测的是,这是由于网络上的打印机/驱动程序错误造成的.我还没有在受影响的系统上重新启用打印,因为我们正在寻找坏的打印机.