From 92a1495ed749dba37c54a61992f6be451541ea1d Mon Sep 17 00:00:00 2001 From: Noah Falk Date: Wed, 25 Sep 2024 21:25:17 -0700 Subject: [PATCH 1/2] Root the System.Runtime EventSource The System.Runtime EventSource (RuntimeEventSource), was unintentionally being garbage collected because it wasn't rooted. This caused runtime EventCounters to no longer be available after GC occurred. This was a regression from a recent change (https://github.com/dotnet/runtime/pull/106014). That change accidentally converted the static field that was intended to the root the object into a property getter that returned a new instance each time it was called. This fix converts the property back into a statically initialized field. This will fix #107919 once it is backported. --- .../src/System/Diagnostics/Tracing/RuntimeEventSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs index 558507211b17f4..255d2edbebf5e6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs @@ -23,7 +23,7 @@ public static class Keywords public const EventKeywords ProcessorCount = (EventKeywords)0x2; } - internal static RuntimeEventSource? Log => new RuntimeEventSource(); + internal static RuntimeEventSource? Log = new RuntimeEventSource(); private PollingCounter? _gcHeapSizeCounter; private IncrementingPollingCounter? _gen0GCCounter; private IncrementingPollingCounter? _gen1GCCounter; From 12f0db9b5edc8a8f3d1057c99f4438726356eabe Mon Sep 17 00:00:00 2001 From: Noah Falk Date: Thu, 26 Sep 2024 14:45:01 -0700 Subject: [PATCH 2/2] PR feedback --- .../src/System/Diagnostics/Tracing/RuntimeEventSource.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs index 255d2edbebf5e6..da0a5e6e437b58 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs @@ -23,7 +23,8 @@ public static class Keywords public const EventKeywords ProcessorCount = (EventKeywords)0x2; } - internal static RuntimeEventSource? Log = new RuntimeEventSource(); + // this roots the singleton instance of the event source + internal static RuntimeEventSource Log { get; } = new RuntimeEventSource(); private PollingCounter? _gcHeapSizeCounter; private IncrementingPollingCounter? _gen0GCCounter; private IncrementingPollingCounter? _gen1GCCounter;