Clean up IRuntime instance flushing (#3895)
authorMike McLaughlin <mikem@microsoft.com>
Mon, 22 May 2023 17:22:18 +0000 (10:22 -0700)
committerGitHub <noreply@github.com>
Mon, 22 May 2023 17:22:18 +0000 (10:22 -0700)
* Fix ClrRuntime flushing which is causing random SOS test failures in gcroot

* Code review feedback

* More changes

* Fix failures

src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs
src/Microsoft.Diagnostics.DebugServices.Implementation/RuntimeProvider.cs

index 40b237f88410628b5cb3ef00493521c2c3dca294..771e0d14c2a83bf5db361b40980e27d7a72da882 100644 (file)
@@ -20,7 +20,6 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
     public class Runtime : IRuntime, IDisposable
     {
         private readonly ClrInfo _clrInfo;
-        private readonly IDisposable _onFlushEvent;
         private readonly ISymbolService _symbolService;
         private Version _runtimeVersion;
         private string _dacFilePath;
@@ -52,24 +51,19 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
             _serviceContainer.AddService<IRuntime>(this);
             _serviceContainer.AddService(clrInfo);
 
-            _onFlushEvent = Target.OnFlushEvent.Register(Flush);
-
             Trace.TraceInformation($"Created runtime #{id} {clrInfo.Flavor} {clrInfo}");
         }
 
         void IDisposable.Dispose()
-        {
-            _serviceContainer.RemoveService(typeof(IRuntime));
-            _serviceContainer.DisposeServices();
-            _onFlushEvent.Dispose();
-        }
-
-        private void Flush()
         {
             if (_serviceContainer.TryGetCachedService(typeof(ClrRuntime), out object service))
             {
-                ((ClrRuntime)service).FlushCachedData();
+                // The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime
+                // instance is disposed below in DisposeServices().
+                ((ClrRuntime)service).DataTarget.Dispose();
             }
+            _serviceContainer.RemoveService(typeof(IRuntime));
+            _serviceContainer.DisposeServices();
         }
 
         #region IRuntime
index a755a13d6dc16441d72bb26c711ae5e4c555c7c7..3ea0ab90be54efd2336a1ff59249f9c593130d35 100644 (file)
@@ -28,6 +28,9 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
         /// <param name="startingRuntimeId">The starting runtime id for this provider</param>
         public IEnumerable<IRuntime> EnumerateRuntimes(int startingRuntimeId)
         {
+            // The ClrInfo and DataTarget instances are disposed when Runtime instance is disposed. Runtime instances are
+            // not flushed when the Target/RuntimeService is flushed; they are all disposed and the list cleared. They are
+            // all re-created the next time the IRuntime or ClrRuntime instance is queried.
             DataTarget dataTarget = new(new CustomDataTarget(_services.GetService<IDataReader>()))
             {
                 FileLocator = null