From 3b1a03b82f44199e36bf8a9eba8c00dcd56b2c46 Mon Sep 17 00:00:00 2001 From: Rajkumar Rangaraj Date: Thu, 28 Mar 2024 23:09:16 -0700 Subject: [PATCH 1/4] Fix runtime store file load issue. --- .../Loader.Net.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs index 3f92b86149..83b18aca61 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs +++ b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs @@ -13,6 +13,8 @@ internal partial class Loader { internal static System.Runtime.Loader.AssemblyLoadContext DependencyLoadContext { get; } = new ManagedProfilerAssemblyLoadContext(); + internal static string[]? StoreFiles { get; } = GetStoreFiles(); + private static string ResolveManagedProfilerDirectory() { string tracerFrameworkDirectory = "net"; @@ -21,6 +23,29 @@ private static string ResolveManagedProfilerDirectory() return Path.Combine(tracerHomeDirectory, tracerFrameworkDirectory); } + private static string[]? GetStoreFiles() + { + try + { + var storeDirectory = Environment.GetEnvironmentVariable("DOTNET_SHARED_STORE"); + if (storeDirectory == null || !Directory.Exists(storeDirectory)) + { + return null; + } + + var architecture = Environment.Is64BitProcess ? "x64" : "x86"; + var targetFramework = $"net{Environment.Version.Major}.{Environment.Version.Minor}"; + var finalPath = Path.Combine(storeDirectory, architecture, targetFramework); + + var storeFiles = Directory.GetFiles(finalPath, "Microsoft.Extensions*.dll", SearchOption.AllDirectories); + return storeFiles; + } + catch + { + return null; + } + } + private static Assembly? AssemblyResolve_ManagedProfilerDependencies(object? sender, ResolveEventArgs args) { var assemblyName = new AssemblyName(args.Name); @@ -56,8 +81,16 @@ private static string ResolveManagedProfilerDirectory() Logger.Debug("Loading {0} with DependencyLoadContext.LoadFromAssemblyPath", path); return DependencyLoadContext.LoadFromAssemblyPath(path); // Load unresolved framework and third-party dependencies into a custom Assembly Load Context } + else + { + var entry = StoreFiles?.FirstOrDefault(e => e.EndsWith($"{assemblyName.Name}.dll")); + if (entry != null) + { + return DependencyLoadContext.LoadFromAssemblyPath(entry); + } - return null; + return null; + } } } #endif From 7d2e785eeb4660099bd1ba5660c2d2fb2a866538 Mon Sep 17 00:00:00 2001 From: Rajkumar Rangaraj Date: Fri, 29 Mar 2024 09:40:21 -0700 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b676daa05..18dc070a81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,14 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h ### Fixed +* Resolved a crash issue caused by `System.IO.FileLoadException` for + `Microsoft.Extensions*.dll` libraries. This issue was due to a conflict with + runtime store libraries, impacting applications with mismatched dependency + versions. This fix enhances stability by addressing the underlying + compatibility concerns. For details see: + ([#3075](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3075), + [#3075](https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/issues/3168)) + ## [1.4.0](https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/tag/v1.4.0) ### Added From 7fcbe91bac66e5a9d7ba91f95f36333079c4ce43 Mon Sep 17 00:00:00 2001 From: Rajkumar Rangaraj Date: Fri, 29 Mar 2024 09:44:28 -0700 Subject: [PATCH 3/4] Fix markdownlint --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18dc070a81..78c3a8dce5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h ### Fixed -* Resolved a crash issue caused by `System.IO.FileLoadException` for +- Resolved a crash issue caused by `System.IO.FileLoadException` for `Microsoft.Extensions*.dll` libraries. This issue was due to a conflict with runtime store libraries, impacting applications with mismatched dependency versions. This fix enhances stability by addressing the underlying From f2410d6d882fa05040e0319ba66e4d3e801b5ed5 Mon Sep 17 00:00:00 2001 From: Rajkumar Rangaraj Date: Fri, 29 Mar 2024 11:07:13 -0700 Subject: [PATCH 4/4] Add arm64 --- .../Loader.Net.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs index 83b18aca61..5a5b4d2fcc 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs +++ b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs @@ -3,6 +3,7 @@ #if NETCOREAPP using System.Reflection; +using System.Runtime.InteropServices; namespace OpenTelemetry.AutoInstrumentation.Loader; @@ -33,7 +34,13 @@ private static string ResolveManagedProfilerDirectory() return null; } - var architecture = Environment.Is64BitProcess ? "x64" : "x86"; + var architecture = RuntimeInformation.ProcessArchitecture switch + { + Architecture.X86 => "x86", + Architecture.Arm64 => "arm64", + _ => "x64" // Default to x64 for architectures not explicitly handled + }; + var targetFramework = $"net{Environment.Version.Major}.{Environment.Version.Minor}"; var finalPath = Path.Combine(storeDirectory, architecture, targetFramework);