-
Notifications
You must be signed in to change notification settings - Fork 636
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
DYN-7691: Add assembly load contexts to packages #15424
base: master
Are you sure you want to change the base?
Changes from 16 commits
f314596
754a48b
ff6a99a
a1a2ecb
717b6a1
bd259e4
19d4f2f
241081b
196b596
111587f
7cd30d6
da7753f
d4bbd2b
d256457
2fb0e1a
3a23e80
d8d91fe
b90f7c0
561919b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Loader; | ||
|
||
namespace Dynamo.PackageManager | ||
{ | ||
internal class PkgAssemblyLoadContext : AssemblyLoadContext | ||
{ | ||
private readonly string RootDir; | ||
private IEnumerable<FileInfo> pkgAssemblies = null; | ||
public PkgAssemblyLoadContext(string name, string pkgRoot, bool unloadable = true) : base(name, unloadable) | ||
{ | ||
this.RootDir = pkgRoot; | ||
} | ||
|
||
protected override Assembly Load(AssemblyName assemblyName) | ||
{ | ||
pkgAssemblies ??= new DirectoryInfo(RootDir).EnumerateFiles("*.dll", new EnumerationOptions() { RecurseSubdirectories = true }); | ||
|
||
var targetAssemName = assemblyName.Name + ".dll"; | ||
var targetAssembly = pkgAssemblies.FirstOrDefault(x => x.Name == targetAssemName); | ||
if (targetAssembly != null) | ||
{ | ||
return LoadFromAssemblyPath(targetAssembly.FullName); | ||
} | ||
return null; | ||
} | ||
|
||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the fact that you can override this function mean that you can also load unmanaged assemblies in isolation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly no, you can only override the search algorithm |
||
{ | ||
pkgAssemblies ??= new DirectoryInfo(RootDir).EnumerateFiles("*.dll", new EnumerationOptions() { RecurseSubdirectories = true }); | ||
|
||
var targetAssemName = unmanagedDllName + ".dll"; | ||
var targetAssembly = pkgAssemblies.FirstOrDefault(x => x.Name == targetAssemName); | ||
if (targetAssembly != null) | ||
{ | ||
return NativeLibrary.Load(targetAssembly.FullName); | ||
} | ||
return IntPtr.Zero; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.IO.Packaging; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using System.Threading; | ||
using Dynamo.Configuration; | ||
using Dynamo.Core; | ||
|
@@ -12,7 +14,10 @@ | |
using Dynamo.Graph.Nodes.CustomNodes; | ||
using Dynamo.Graph.Workspaces; | ||
using Dynamo.Interfaces; | ||
using Dynamo.Models; | ||
using Dynamo.Scheduler; | ||
using Dynamo.Search.SearchElements; | ||
using DynamoUtilities; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
|
@@ -644,7 +649,66 @@ public void PlacingCustomNodeInstanceFromPackageRetainsCorrectPackageInfoState() | |
loader.RequestLoadNodeLibrary -= libraryLoader.LoadLibraryAndSuppressZTSearchImport; | ||
} | ||
|
||
|
||
[Test] | ||
public void LoadPackagesInAssemblyIsolation() | ||
{ | ||
// Needed for FeatureFlags | ||
Assert.IsTrue(DynamoModel.IsTestMode); | ||
Assert.AreEqual("Package1,Package2,Package", DynamoModel.FeatureFlags.CheckFeatureFlag<string>("IsolatePackages", "")); | ||
Assert.AreEqual("Package", DynamoModel.FeatureFlags.CheckFeatureFlag<string>("DoNotIsolatePackages", "")); | ||
|
||
|
||
var loader = GetPackageLoader(); | ||
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel); | ||
|
||
loader.PackagesLoaded += libraryLoader.LoadPackages; | ||
|
||
var packageDirectory = Path.Combine(TestDirectory, "testAssemblyIsolation", "Package1"); | ||
var packageDirectory2 = Path.Combine(TestDirectory, "testAssemblyIsolation", "Package2"); | ||
var packageDirectory3 = Path.Combine(TestDirectory, "pkgs", "Package"); | ||
var package1 = Package.FromDirectory(packageDirectory, CurrentDynamoModel.Logger); | ||
var package2 = Package.FromDirectory(packageDirectory2, CurrentDynamoModel.Logger); | ||
var package3 = Package.FromDirectory(packageDirectory3, CurrentDynamoModel.Logger); | ||
loader.LoadPackages([package1, package2, package3]); | ||
|
||
loader.PackagesLoaded -= libraryLoader.LoadPackages; | ||
|
||
// 2 packages loaded as expected | ||
var expectedLoadedPackageNum = 0; | ||
foreach (var pkg in loader.LocalPackages) | ||
{ | ||
if (pkg.Name == "Package1" || pkg.Name == "Package2" || pkg.Name == "Package") | ||
{ | ||
expectedLoadedPackageNum++; | ||
} | ||
} | ||
Assert.AreEqual(3, expectedLoadedPackageNum); | ||
|
||
string openPath = Path.Combine(TestDirectory, @"testAssemblyIsolation\graph.dyn"); | ||
RunModel(openPath); | ||
|
||
var expectedVersions = 0; | ||
var pkgLoadContexts = AssemblyLoadContext.All.Where(x => x.Name.Equals("[email protected]") || x.Name.Equals("[email protected]")).ToList(); | ||
foreach (var pkg in pkgLoadContexts) | ||
{ | ||
var dep = pkg.Assemblies.FirstOrDefault(x => x.GetName().Name == "Newtonsoft.Json"); | ||
Assert.IsNotNull(dep); | ||
|
||
var ver = dep.GetName().Version.ToString(); | ||
// Expected both versions of Newtonsoft to be loaded | ||
if (ver == "13.0.0.0" || ver == "8.0.0.0") | ||
{ | ||
expectedVersions++; | ||
} | ||
} | ||
Assert.AreEqual(2, expectedVersions); | ||
|
||
// Make sure the "DoNotIsolatePackages" fflag puts the pacakge in the default load context(i.e does not isolate it) | ||
var contexts = AssemblyLoadContext.All.Where(l => l.Assemblies.FirstOrDefault(x => x.GetName().Name == "Package") != null).ToList(); | ||
Assert.AreEqual(1, contexts.Count); | ||
Assert.True(contexts[0] == AssemblyLoadContext.Default); | ||
} | ||
|
||
[Test] | ||
public void LoadingConflictingCustomNodePackageDoesNotGetLoaded() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"license":"","file_hash":null,"name":"Package1","version":"1.0.0","description":"original package","group":"","keywords":null,"dependencies":[],"contents":"","engine_version":"2.1.0.7840","engine":"dynamo","engine_metadata":"","site_url":"","repository_url":"","contains_binaries":true,"node_libraries":["TestAssemblyIsolation1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"license":"","file_hash":null,"name":"Package2","version":"1.0.0","description":"original package","group":"","keywords":null,"dependencies":[],"contents":"","engine_version":"2.1.0.7840","engine":"dynamo","engine_metadata":"","site_url":"","repository_url":"","contains_binaries":true,"node_libraries":["TestAssemblyIsolation2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this needs to be isolated or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
umm, certainly seems so? I guess this should use an MLC to determine what type of binary it is before loading it into the real ALC for this package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh - this is just for publishing, I mean, I still think what I stated is the way to go, but maybe less important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might add a new task for this