From 9dec7cd785ff947dd8f78d3cd29e1d9ad2d0a360 Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Mon, 10 Jul 2023 14:08:09 -0700 Subject: [PATCH] Misc fix for issue that Juan hit (#4053) --- .../Runtime.cs | 13 +++++------ .../ServiceContainer.cs | 22 +++++-------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs index 771e0d14c..04325022b 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs @@ -22,6 +22,7 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation private readonly ClrInfo _clrInfo; private readonly ISymbolService _symbolService; private Version _runtimeVersion; + private ClrRuntime _clrRuntime; private string _dacFilePath; private string _dbiFilePath; @@ -56,12 +57,10 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation void IDisposable.Dispose() { - if (_serviceContainer.TryGetCachedService(typeof(ClrRuntime), out object service)) - { - // The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime - // instance is disposed below in DisposeServices(). - ((ClrRuntime)service).DataTarget.Dispose(); - } + // The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime + // instance is disposed below in DisposeServices(). + _clrRuntime?.DataTarget.Dispose(); + _clrRuntime = null; _serviceContainer.RemoveService(typeof(IRuntime)); _serviceContainer.DisposeServices(); } @@ -125,7 +124,7 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation { // Ignore the DAC version mismatch that can happen because the clrmd ELF dump reader // returns 0.0.0.0 for the runtime module that the DAC is matched against. - return _clrInfo.CreateRuntime(dacFilePath, ignoreMismatch: true); + return _clrRuntime = _clrInfo.CreateRuntime(dacFilePath, ignoreMismatch: true); } catch (Exception ex) when (ex is DllNotFoundException or diff --git a/src/Microsoft.Diagnostics.DebugServices/ServiceContainer.cs b/src/Microsoft.Diagnostics.DebugServices/ServiceContainer.cs index d51b4d8ca..9b497c53f 100644 --- a/src/Microsoft.Diagnostics.DebugServices/ServiceContainer.cs +++ b/src/Microsoft.Diagnostics.DebugServices/ServiceContainer.cs @@ -73,30 +73,18 @@ namespace Microsoft.Diagnostics.DebugServices } /// - /// Get the cached/instantiated service instance if one exists. Don't call the factory or parent to create. + /// Returns the instance of the service or returns null if service doesn't exist /// /// service type - /// service instance (can be null) - /// if true, found service - public bool TryGetCachedService(Type type, out object service) + /// service instance or null + public object GetService(Type type) { Debug.Assert(type != null); if (type == typeof(IServiceProvider)) { - service = this; - return true; + return this; } - return _instances.TryGetValue(type, out service); - } - - /// - /// Returns the instance of the service or returns null if service doesn't exist - /// - /// service type - /// service instance or null - public object GetService(Type type) - { - if (TryGetCachedService(type, out object service)) + if (_instances.TryGetValue(type, out object service)) { return service; } -- 2.34.1