English 中文(简体)
新的SackTrace(Thread, bool) hangs 到达InfoCache<T>。
原标题:new StackTrace(Thread, bool) hangs when reaches MemberInfoCache<T>.AddMethod()

I am writing code implementing some kind of "in-process profiler" component in our multiuser multithread application server. It is basically working perfect - showing stacktraces per thread with some additional information such as CPU utilization for this thread, SQL wait times etc., refreshing every X seconds. I nearly checked-in, but suddenly I noticed in debug session that my application hanged! It happens not all the time, but randomally, hinting on some concurrency problems. After close examination, I ve seen that application hanged during call to "new StackTrace(one_of_my_threads, false)". Here is debugger s stack:

[Managed to Native Transition]  
mscorlib.dll!System.RuntimeType.RuntimeTypeCache.MemberInfoCache<System.Reflection.RuntimeMethodInfo>.AddMethod(System.RuntimeTypeHandle declaringType = {System.RuntimeTypeHandle}, System.RuntimeMethodHandle method, System.RuntimeType.RuntimeTypeCache.CacheType cacheType) + 0x88 bytes   
mscorlib.dll!System.RuntimeType.RuntimeTypeCache.GetMethod(System.RuntimeTypeHandle declaringType, System.RuntimeMethodHandle method) + 0x2d bytes  
mscorlib.dll!System.RuntimeType.GetMethodBase(System.RuntimeTypeHandle reflectedTypeHandle, System.RuntimeMethodHandle methodHandle) + 0xf5 bytes   
mscorlib.dll!System.Diagnostics.StackFrameHelper.GetMethodBase(int i) + 0x4e bytes  
mscorlib.dll!System.Diagnostics.StackTrace.CaptureStackTrace(int iSkip, bool fNeedFileInfo = false, System.Threading.Thread targetThread, System.Exception e = null) + 0xb8 bytes   

mscorlib.dll!System.Diagnostics.StackTrace.StackTrace(System.Threading.Thread targetThread, bool needFileInfo) + 0x18 bytes 

>Almog.Next.Tools.dll!Almog.Next.Services.ThreadWorkUnit.GetStackTrace(System.Threading.Thread th = {System.Threading.Thread}, Almog.Next.Services.StackDetalization details = Simplified) Line 175 + 0x2c bytes    

Almog.Next.Tools.dll!Almog.Next.Services.ThreadWorkUnit.InternalCalculate(Almog.Next.Services.StackDetalization sdetails = Simplified) Line 131 + 0x1b bytes

Almog.Next.Tools.dll!Almog.Next.Services.ThreadWorkUnit.Calculate(Almog.Next.Services.StackDetalization sdetails = Simplified) Line 90 + 0xc bytes

Almog.Next.Tools.dll!Almog.Next.Services.InProcessProfiler.GetThreadWorks(Almog.Next.Services.StackDetalization stackLevel = Simplified) Line 41 + 0xe bytes

Almog.Next.Tools.dll!Almog.Next.CommonControls.ShowWorkloadForm.timer_Tick(object sender = {Interval = 250}, System.EventArgs e = {System.EventArgs}) Line 40 + 0x9 bytes

System.Windows.Forms.dll!System.Windows.Forms.Timer.OnTick(System.EventArgs e) + 0x17 bytes 

System.Windows.Forms.dll!System.Windows.Forms.Timer.TimerNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x34 bytes  

System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg = 275, System.IntPtr wparam, System.IntPtr lparam) + 0x5a bytes 

[Native to Managed Transition]  

[Managed to Native Transition]  

System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e bytes 

System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x177 bytes 

System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    

System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes  

NextServer.exe!Almog.Next.Server.Program.Main(string[] ParamStr = {string[0]}) Line 24 + 0x1d bytes

我也关注到,在分配新物体(称为“新”)时,其他透镜也受阻。 现行例子之一,沿这一法典行:

return new TransactionController(this, transactionMode, isolationLevel);

I believe that every "new" has something to do with MemberInfoCache, but can t figure out what I could do even after looking into AddMethod() s code with Reflector. Please, help! I am completely stuck.

Here is the code reading stacktrace. Nothing unusual, imho: ...

if (th == Thread.CurrentThread)
                throw new NextException("Internal error: it seems the thread {0} once called BeginWork() is the same thread which calls GetThreadWorks() now...", th.ManagedThreadId);
pragma warning disable 612, 618
            th.Suspend();
            var trace = new StackTrace(th, details == StackDetalization.Detailed);
            th.Resume();
pragma warning restore 612, 618
            var sb = new StringBuilder();
            int frameCount = trace.FrameCount;
            for (int i = 0; i < frameCount; i++)
            {                

...

预告......

问题回答

如果你停下来的路子锁,StackTrace正在使用这一锁锁,你将陷入僵局。

Using Thread.Suspend is deprecated for a reason: You basically cannot safely use it. You can only use it if you know exactly what code the suspended thread is running, including any BCL stuff. You cannot know that.

我不知道有解决办法。 我猜测,为了抓捕 st物,暂停使用needs?





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签