Skip to content

Possible finalizer thread stuck on System.RuntimeMethodHandle.Destroy #31782

Closed
@vtortola

Description

@vtortola

Description

When a single application instance reaches ~1700 Rps and ~85% Cpu usage during a short period of time due a traffic spike, around a 25% of our servers experiment what it seems to be a blockage in the finalizer thread and the application starts hoarding memory and increasing latency. Eventually we have to kill the server when memory is at 10X of what it uses normally and latency is not acceptable.

Exploring with dotnet-dump, we see that after the requests spike the finalizer queue starts to accumulate objects. Taking multiple dumps shows that the "Ready for finalization" keep growing in each heap.

When exploring the threads, there is always a thread 0x001B that is stuck in this frame:

> clrstack -a                                                                                                                                                                                                                     
OS Thread Id: 0x1b (21)
        Child SP               IP Call Site
00007F0002EC9930 00007f00081947b2 [InlinedCallFrame: 00007f0002ec9930] 
00007F0002EC9930 00007eff88696957 [InlinedCallFrame: 00007f0002ec9930] 
00007F0002EC9920 00007EFF88696957 System.RuntimeMethodHandle.Destroy(System.RuntimeMethodHandleInternal)
    PARAMETERS:
        method = <no data>

00007F0002EC99C0 00007EFF9623E087 System.Reflection.Emit.DynamicResolver+DestroyScout.Finalize() [/_/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs @ 669]
    PARAMETERS:
        this = <no data>

00007F0002EC9D30 00007f0007957cc6 [DebuggerU2MCatchHandlerFrame: 00007f0002ec9d30] 

If I take a dump 30 minutes later, it still shows this frame with this same data in this thread.

Those servers keep having a higher % of time in GC
image

Configuration

Server is an Azure Standard_F16s_v2 (16 cores, 32 GiB RAM).

Docker image mcr.microsoft.com/dotnet/aspnet:7.0.11.

# dotnet --info                                                                                                                                                                               

Host:
  Version:      7.0.11
  Architecture: x64
  Commit:       ecb34f85ec

.NET SDKs installed:
  No SDKs were found.

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

# uname -a
Linux live-api-75fcf998d-hcwvz 5.15.0-1041-azure dotnet/runtime#48-Ubuntu SMP Tue Jun 20 20:34:08 UTC 2023 x86_64 GNU/Linux

Regression?

We are not completely sure, but we have the feeling it started happening when we moved to .NET7 from .NET6.
When we were in .NET6 we did not do see this kind of situation. After we moved to .NET7 we started seeing some machines using an unexpected big amount of memory that starts on a traffic spike situation.

Data

> syncblk                                                                                                                                                                                                                         
Index         SyncBlock MonitorHeld Recursion Owning Thread Info          SyncBlock Owner
-----------------------------
Total           2661
Free            793

> finalizequeue -stat                                                                                                                                                                                                             
SyncBlocks to be cleaned up: 0
Free-Threaded Interfaces to be released: 0
MTA Interfaces to be released: 0
STA Interfaces to be released: 0
----------------------------------

Heap 0
generation 0 has 29 objects (7ef79c121920->7ef79c121a08)
generation 1 has 2,464 objects (7ef79c11cc20->7ef79c121920)
generation 2 has 0 objects (7ef79c11cc20->7ef79c11cc20)
Ready for finalization 560 objects (7ef79c123438->7ef79c1245b8)
------------------------------
Heap 1
generation 0 has 5 objects (7ef5f820ada8->7ef5f820add0)
generation 1 has 2,013 objects (7ef5f8206ec0->7ef5f820ada8)
generation 2 has 0 objects (7ef5f8206ec0->7ef5f8206ec0)
Ready for finalization 309 objects (7ef5f820b480->7ef5f820be28)
------------------------------
Heap 2
generation 0 has 7 objects (7ef5bc101488->7ef5bc1014c0)
generation 1 has 1,409 objects (7ef5bc0fe880->7ef5bc101488)
generation 2 has 0 objects (7ef5bc0fe880->7ef5bc0fe880)
Ready for finalization 321 objects (7ef5bc101ea0->7ef5bc1028a8)
------------------------------
Heap 3
generation 0 has 14 objects (7ef6142535b8->7ef614253628)
generation 1 has 1,295 objects (7ef614250d40->7ef6142535b8)
generation 2 has 0 objects (7ef614250d40->7ef614250d40)
Ready for finalization 321 objects (7ef6142540a0->7ef614254aa8)
------------------------------
Heap 4
generation 0 has 2 objects (7ef7a0bbfa88->7ef7a0bbfa98)
generation 1 has 729 objects (7ef7a0bbe3c0->7ef7a0bbfa88)
generation 2 has 0 objects (7ef7a0bbe3c0->7ef7a0bbe3c0)
Ready for finalization 322 objects (7ef7a0bc05c0->7ef7a0bc0fd0)
------------------------------
Heap 5
generation 0 has 10 objects (7eff84129200->7eff84129250)
generation 1 has 992 objects (7eff84127300->7eff84129200)
generation 2 has 0 objects (7eff84127300->7eff84127300)
Ready for finalization 388 objects (7eff84129bd0->7eff8412a7f0)
------------------------------
Heap 6
generation 0 has 5 objects (7ef694166578->7ef6941665a0)
generation 1 has 1,511 objects (7ef694163640->7ef694166578)
generation 2 has 0 objects (7ef694163640->7ef694163640)
Ready for finalization 339 objects (7ef694166fa8->7ef694167a40)
------------------------------
Heap 7
generation 0 has 2 objects (7ef65c27acc0->7ef65c27acd0)
generation 1 has 1,692 objects (7ef65c2777e0->7ef65c27acc0)
generation 2 has 0 objects (7ef65c2777e0->7ef65c2777e0)
Ready for finalization 316 objects (7ef65c27b690->7ef65c27c070)
------------------------------
Heap 8
generation 0 has 5 objects (7ef79c180c20->7ef79c180c48)
generation 1 has 594 objects (7ef79c17f990->7ef79c180c20)
generation 2 has 0 objects (7ef79c17f990->7ef79c17f990)
Ready for finalization 376 objects (7ef79c1818f0->7ef79c1824b0)
------------------------------
Heap 9
generation 0 has 7 objects (7ef770377290->7ef7703772c8)
generation 1 has 832 objects (7ef770375890->7ef770377290)
generation 2 has 0 objects (7ef770375890->7ef770375890)
Ready for finalization 388 objects (7ef770377bd0->7ef7703787f0)
------------------------------
Heap 10
generation 0 has 10 objects (7ef79c245ce0->7ef79c245d30)
generation 1 has 804 objects (7ef79c2443c0->7ef79c245ce0)
generation 2 has 0 objects (7ef79c2443c0->7ef79c2443c0)
Ready for finalization 390 objects (7ef79c246408->7ef79c247038)
------------------------------
Heap 11
generation 0 has 8 objects (7ef6881c80c8->7ef6881c8108)
generation 1 has 1,297 objects (7ef6881c5840->7ef6881c80c8)
generation 2 has 0 objects (7ef6881c5840->7ef6881c5840)
Ready for finalization 327 objects (7ef6881c8b78->7ef6881c95b0)
------------------------------
Heap 12
generation 0 has 10 objects (7ef79c2af180->7ef79c2af1d0)
generation 1 has 2,222 objects (7ef79c2aac10->7ef79c2af180)
generation 2 has 0 objects (7ef79c2aac10->7ef79c2aac10)
Ready for finalization 485 objects (7ef79c2afc38->7ef79c2b0b60)
------------------------------
Heap 13
generation 0 has 13 objects (7ef74815af60->7ef74815afc8)
generation 1 has 350 objects (7ef74815a470->7ef74815af60)
generation 2 has 0 objects (7ef74815a470->7ef74815a470)
Ready for finalization 329 objects (7ef74815b948->7ef74815c390)
------------------------------
Heap 14
generation 0 has 8 objects (7ef79c79e070->7ef79c79e0b0)
generation 1 has 322 objects (7ef79c79d660->7ef79c79e070)
generation 2 has 0 objects (7ef79c79d660->7ef79c79d660)
Ready for finalization 25,489 objects (7ef79c79e9b8->7ef79c7d0640)
------------------------------
Heap 15
generation 0 has 7 objects (7ef6802dadd0->7ef6802dae08)
generation 1 has 508 objects (7ef6802d9df0->7ef6802dadd0)
generation 2 has 0 objects (7ef6802d9df0->7ef6802d9df0)
Ready for finalization 404,766 objects (7ef6802db588->7ef6805f1e78)
------------------------------
Statistics for all finalizable objects (including all objects ready for finalization):
Statistics:
          MT   Count  TotalSize Class Name

[...]

7eff8de90098     538     17,216 Microsoft.Win32.SafeHandles.SafeEvpMdCtxHandle
7eff917fa6f8      70     18,480 Microsoft.Data.SqlClient.SNI.SNISslStream
7eff90117f28     355     19,880 System.Runtime.CompilerServices.ConditionalWeakTable<System.Text.Json.JsonSerializerOptions, System.Object>+Container
7eff9433c1f0     361     20,216 System.IO.Compression.Deflater
7eff8c2c7b88     316     20,224 System.Net.Sockets.SafeSocketHandle
7eff8d8ce2c8     663     21,216 Microsoft.Win32.SafeHandles.SafeX509Handle
7eff8d830920   1,255     30,120 System.Threading.TimerHolder
7eff9433efe8     394     31,520 System.IO.Compression.ZLibNative+ZLibStreamHandle
7eff8c2c5228     316     37,920 System.Net.Sockets.Socket
7eff927f5370     180     44,640 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketReceiver
7eff8d859958     223     62,440 System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs
7eff9146acb8     389     80,912 Microsoft.Data.SqlClient.SqlConnection
7eff9149f348     980    290,080 Microsoft.Data.SqlClient.SqlCommand
7eff89253d70   4,192    301,824 System.Threading.Thread
7eff90b81468 247,708  5,944,992 System.Reflection.Emit.DynamicResolver+DestroyScout
7eff8ad24450 199,413 14,357,736 System..Reflection.Emit.DynamicResolver
Total 460,012 objects, 21,441,768 bytes
> dumpheap -stat

[...]

7eff8acfc688   207,010   9,936,480 System.Reflection.Emit.DynamicMethod+RTDynamicMethod
7eff8ad200a8   207,010   9,936,480 System.Reflection.Emit.ScopeTree
7eff8acfcf68   208,325   9,999,600 System.Reflection.Emit.SignatureHelper
7eff8919ec38   605,088  14,522,112 System.RuntimeTypeHandle
7eff8ad24450   207,001  14,904,072 System.Reflection.Emit.DynamicResolver
7eff89817a20   212,441  20,394,336 System.RuntimeMethodInfoStub
7eff8acfbda0   207,010  23,185,120 System.Reflection.Emit.DynamicMethod
7eff8912a508   358,123  25,959,536 System.Object[]
7eff8acfb6e8   207,010  31,465,520 System.Reflection.Emit.DynamicILGenerator
7eff89251038 1,241,746 120,095,736 System.String
7eff89773338 1,070,259 136,469,834 System.Byte[]
7eff892b7aa8    90,512 260,066,802 System.Char[]
55fb5eb4e900    55,769 364,019,016 Free
Total 10,141,843 objects, 1,321,079,065 bytes
[System.Runtime]
    % Time in GC since last GC (%)                                         5    
    Allocation Rate (B / 1 sec)                                       6.2155e+08   
    CPU Usage (%)                                                         57.81 
    Exception Count (Count / 1 sec)                                       94    
    GC Committed Bytes (MB)                                            1,675.366
    GC Fragmentation (%)                                                  44.356
    GC Heap Size (MB)                                                    637.815
    Gen 0 GC Count (Count / 1 sec)                                         2    
    Gen 0 Size (B)                                                 4,236,872    
    Gen 1 GC Count (Count / 1 sec)                                         1    
    Gen 1 Size (B)                                                11,126,616    
    Gen 2 GC Count (Count / 1 sec)                                         0    
    Gen 2 Size (B)                                                    5.2245e+08   
    IL Bytes Jitted (B)                                               3.1923e+08   
    LOH Size (B)                                                      5.7157e+08   
    Monitor Lock Contention Count (Count / 1 sec)                      1,212    
    Number of Active Timers                                               46    
    Number of Assemblies Loaded                                          252    
    Number of Methods Jitted                                       9,647,443    
    POH (Pinned Object Heap) Size (B)                              4,465,760    
    ThreadPool Completed Work Item Count (Count / 1 sec)              23,982    

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions