【问题标题】:How to list running tasks in .net memory dump如何在 .net 内存转储中列出正在运行的任务
【发布时间】:2017-06-01 15:22:54
【问题描述】:

我们有一个使用 async/await 模式的复杂 ASP.Net Core 应用程序。该应用程序最近停止响应,我们为它进行了内存转储。我们怀疑有一些异步操作导致应用卡住,但不确定是哪一个。在为 Web 应用程序进行内存转储后,我们可以看到运行线程非常少,因为线程由于使用 async/await 而返回到线程池。 问题是,是否可以在内存转储中列出正在运行的任务以及它们运行到的位置,以便我可以判断哪个异步操作使应用程序卡住了?对于同步阻塞调用,这很容易——只需列出所有活动线程的调用堆栈。但是对于异步操作,它不再起作用了。 (添加更多跟踪是一种可能的方法,但情况是我们无法保证应用程序中的每个异步操作及其依赖库都有足够的跟踪。)

例如,如果一个 ASP.Net Core 应用程序卡在类似这样的代码中,我如何从内存转储中判断它?

public async Task SomeBadMethodInADependentLibrary()
{
    TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
    await tcs.Task;
}

【问题讨论】:

    标签: .net multithreading asynchronous windbg sos


    【解决方案1】:

    您当然可以在堆中找到任务对象并开始使用 SOS 命令手动分析它们,例如就像调试会话的开始:

    0:013> !dumpheap -stat -type Task
    Statistics:
          MT    Count    TotalSize Class Name
    [...]
    71e03f28        4          160 System.Threading.Tasks.Task
    Total 28 objects
    
    0:013> !dumpheap -mt 71e03f28 
     Address       MT     Size
    022bd900 71e03f28       40       
    [...]
    
    0:013> !do 022bd900
    Name:        System.Threading.Tasks.Task
    MethodTable: 71e03f28
    EEClass:     719cd6e0
    Size:        40(0x28) bytes
    File:        C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
    Fields:
          MT    Field   Offset                 Type VT     Attr    Value Name
    71df1638  40019cd       1c         System.Int32  1 instance        3 m_taskId
    71defb44  40019ce        4        System.Object  0 instance 022bd8e0 m_action
    [...]
    
    0:013> !DumpObj 022bd8e0
    Name:        System.Action
    MethodTable: 71e0588c
    EEClass:     719357b8
    Size:        32(0x20) bytes
    File:        C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
    Fields:
          MT    Field   Offset                 Type VT     Attr    Value Name
    71defb44  40002b5        4        System.Object  0 instance 022bd8e0 _target
    71defb44  40002b6        8        System.Object  0 instance 00000000 _methodBase
    71df2bdc  40002b7        c        System.IntPtr  1 instance  4b00e64 _methodPtr
    71df2bdc  40002b8       10        System.IntPtr  1 instance   4e0c30 _methodPtrAux
    [...]
    
    0:013> !u 4e0c30 
    Unmanaged code
    004e0c30 e833df8372      call    clr!PrecodeFixupThunk (72d1eb68)
    [...]
    

    现在它开始变得很麻烦......

    在我看来,最方便的方法(在 WinDbg 中)是使用 Mex (Github)!TaskTriage 命令:

    0:013> !TaskTriage
    Normal Mode - not showing successful Tasks
    Address    Target     Status                        Method                             Exceptions
    ==================================================================================================
    022bd900 | 022bd8e0 | TASK_STATE_DELEGATE_INVOKED | Demo.Program.printMessage()     |     <none>
    022bd974 | 022bd868 | TASK_STATE_DELEGATE_INVOKED | Demo.Program+<>c.<Main>b__0_0() |     <none>
    022bd9bc | 022bd868 | TASK_STATE_STARTED          | Demo.Program+<>c.<Main>b__0_1() |     <none>
    022bda04 | 022bd868 | TASK_STATE_STARTED          | Demo.Program+<>c.<Main>b__0_2() |     <none>
    ==================================================================================================
    Address    Target     Status                        Method                             Exceptions
    

    在 Visual Studio 上使用 WinDbg 的想法很好,因为 VS2015 和 VS2017 都无法从转储文件中给出相同的结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 2012-11-09
      相关资源
      最近更新 更多