Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve file loading issues originating from Runtime store libraries #3343

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if NETCOREAPP
using System.Reflection;
using System.Runtime.InteropServices;

namespace OpenTelemetry.AutoInstrumentation.Loader;

Expand All @@ -13,6 +14,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";
Expand All @@ -21,6 +24,35 @@ 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 = 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);

var storeFiles = Directory.GetFiles(finalPath, "Microsoft.Extensions*.dll", SearchOption.AllDirectories);
pjanotti marked this conversation as resolved.
Show resolved Hide resolved
return storeFiles;
}
catch
{
return null;
}
}

private static Assembly? AssemblyResolve_ManagedProfilerDependencies(object? sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
Expand Down Expand Up @@ -56,8 +88,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
Loading