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

DYN-7691: Add assembly load contexts to packages #15424

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Forms;
Expand Down Expand Up @@ -2218,7 +2219,7 @@ private void AddDllFile(string filename)
{
// we're not sure if this is a managed assembly or not
// we try to load it, if it fails - we add it as an additional file
var result = PackageLoader.TryLoadFrom(filename, out Assembly assem);
var result = PackageLoader.TryLoadFrom(AssemblyLoadContext.Default, filename, out Assembly assem);
Copy link
Contributor Author

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.

Copy link
Member

@mjkkirschner mjkkirschner Oct 29, 2024

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?

Copy link
Member

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.

Copy link
Contributor Author

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

if (result)
{
var assemName = assem.GetName().Name;
Expand Down
6 changes: 4 additions & 2 deletions src/DynamoPackages/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Dynamo.Core;
using Dynamo.Exceptions;
using Dynamo.Graph.Nodes.CustomNodes;
Expand Down Expand Up @@ -228,6 +229,7 @@ internal IEnumerable<Assembly> NodeLibraries
/// </summary>
internal bool RequiresSignedEntryPoints { get; set; }

internal AssemblyLoadContext AssemblyLoadContext { get; set; } = AssemblyLoadContext.Default;
#endregion

public Package(string directory, string name, string versionName, string license)
Expand Down Expand Up @@ -374,7 +376,7 @@ internal IEnumerable<PackageAssembly> EnumerateAndLoadAssembliesInBinDirectory()
if (shouldLoadFile)
{
// dll files may be un-managed, skip those
var result = PackageLoader.TryLoadFrom(assemFile.FullName, out assem);
var result = PackageLoader.TryLoadFrom(AssemblyLoadContext, assemFile.FullName, out assem);
if (result)
{
// IsNodeLibrary may fail, we store the warnings here and then show
Expand Down Expand Up @@ -586,7 +588,7 @@ internal void UninstallCore(CustomNodeManager customNodeManager, PackageLoader p
if (BuiltInPackage)
{
LoadState.SetAsUnloaded();
Analytics.TrackEvent(Actions.BuiltInPackageConflict, Categories.PackageManagerOperations, $"{Name } {versionName} set unloaded");
Analytics.TrackEvent(Actions.BuiltInPackageConflict, Categories.PackageManagerOperations, $"{Name} {versionName} set unloaded");

RaisePropertyChanged(nameof(LoadState));

Expand Down
46 changes: 46 additions & 0 deletions src/DynamoPackages/PackageAssemblyLoadContext.cs
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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
}
57 changes: 53 additions & 4 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using Dynamo.Core;
using Dynamo.Exceptions;
using Dynamo.Extensions;
Expand Down Expand Up @@ -37,6 +37,49 @@ public class PackageLoader : LogSourceBase
internal event Func<string, IExtension> RequestLoadExtension;
internal event Action<IExtension> RequestAddExtension;

private HashSet<string> packagesToIsolate = null;
private HashSet<string> packagesToNotIsolate = null;

internal bool CanIsolatePackage(string package)
{
static void populateFlags(string fflagKey, ref HashSet<string> pkgs)
{
string flags = DynamoModel.FeatureFlags?.CheckFeatureFlag(fflagKey, string.Empty);
if (!string.IsNullOrEmpty(flags))
{
foreach (var x in flags.Split(","))
{
pkgs.Add(x);
}
}
}

if (packagesToIsolate == null)
{
packagesToIsolate = [];
populateFlags("IsolatePackages", ref packagesToIsolate);
}

if (packagesToNotIsolate == null)
{
packagesToNotIsolate = [];
populateFlags("DoNotIsolatePackages", ref packagesToNotIsolate);
}

// NotIsolate has the highest priority.
if (packagesToNotIsolate.Contains(package) || packagesToNotIsolate.Contains("All"))
{
return false;
}

if (packagesToIsolate.Contains(package) || packagesToIsolate.Contains("All"))
{
return true;
}

return false;
}

/// <summary>
/// This event is raised when a package is first added to the list of packages this package loader is loading.
/// This event occurs before the package is fully loaded.
Expand Down Expand Up @@ -201,7 +244,12 @@ private void TryLoadPackageIntoLibrary(Package package)
{
var dynamoVersion = VersionUtilities.PartialParse(DynamoModel.Version);

List<Assembly> blockedAssemblies = new List<Assembly>();
if (CanIsolatePackage(package.Name))
{
package.AssemblyLoadContext = new PkgAssemblyLoadContext(package.Name + "@" + package.VersionName, package.RootDirectory);
}

List<Assembly> blockedAssemblies = [];
// Try to load node libraries from all assemblies
foreach (var assem in package.EnumerateAndLoadAssembliesInBinDirectory())
{
Expand Down Expand Up @@ -742,14 +790,15 @@ internal static void CleanSharedPublishLoadContext(MetadataLoadContext mlc)
/// <summary>
/// Attempt to load a managed assembly in to LoadFrom context.
/// </summary>
/// <param name="alc"></param>
/// <param name="filename">The filename of a DLL</param>
/// <param name="assem">out Assembly - the passed value does not matter and will only be set if loading succeeds</param>
/// <returns>Returns true if success, false if BadImageFormatException (i.e. not a managed assembly)</returns>
internal static bool TryLoadFrom(string filename, out Assembly assem)
internal static bool TryLoadFrom(AssemblyLoadContext alc, string filename, out Assembly assem)
{
try
{
assem = Assembly.LoadFrom(filename);
assem = alc.LoadFromAssemblyPath(filename);
return true;
}
catch (FileLoadException e)
Expand Down
31 changes: 26 additions & 5 deletions src/DynamoUtilities/DynamoFeatureFlagsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace DynamoUtilities
{

internal interface IFFlags
{
internal T CheckFeatureFlag<T>(DynamoFeatureFlagsManager mgr, string featureFlagKey, T defaultval);
}

/// <summary>
/// A wrapper around the DynamoFeatureFlags CLI tool.
Expand All @@ -20,9 +21,21 @@ namespace DynamoUtilities
/// </summary>
internal class DynamoFeatureFlagsManager : CLIWrapper
{
// Utility class that supports mocking during tests
class FFlags : IFFlags
{
T IFFlags.CheckFeatureFlag<T>(DynamoFeatureFlagsManager mgr, string featureFlagKey, T defaultval)
{
return mgr.CheckFeatureFlagInternal(featureFlagKey, defaultval);
}
}

// Useful for mocking in tests
internal IFFlags flags { get; set; } = new FFlags();
Copy link
Contributor Author

@pinzart90 pinzart90 Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have tunnel vision, but I could not think of anything else to easily enable mocking.
I seem to need mocking because I am using the CheckFeatureFlag function in the PackageLoader

I tried to avoid refactoring the entire DynamoFeatureFlagsManager to be mockable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjkkirschner any other options that you see?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature flags cli already returns a set of static test flags in test mode - is that sufficient?

private string relativePath = Path.Combine("DynamoFeatureFlags", "DynamoFeatureFlags.exe");
private Dictionary<string, object> AllFlagsCache { get; set; }//TODO lock is likely overkill.
private SynchronizationContext syncContext;
private readonly bool testmode = false;
internal static event Action FlagsRetrieved;

//TODO(DYN-6464)- remove this field!.
Expand All @@ -43,8 +56,10 @@ internal class DynamoFeatureFlagsManager : CLIWrapper
/// <param name="syncContext">context used for raising FlagRetrieved event.</param>
/// <param name="testmode">will not contact feature flag service in testmode, will respond with defaults.</param>
internal DynamoFeatureFlagsManager(string userkey, SynchronizationContext syncContext, bool testmode=false)
{
{
this.syncContext = syncContext;
this.testmode = testmode;

//dont pass userkey arg if null/empty
var userkeyarg = $"-u {userkey}";
var testmodearg = string.Empty;
Expand All @@ -62,7 +77,6 @@ internal DynamoFeatureFlagsManager(string userkey, SynchronizationContext syncCo

internal void CacheAllFlags()
{

//wait for response
var dataFromCLI = GetData(featureFlagTimeoutMs);
//convert from json string to dictionary.
Expand Down Expand Up @@ -91,6 +105,13 @@ internal void CacheAllFlags()
/// <param name="defaultval">Currently the flag and default val MUST be a bool or string.</param>
/// <returns></returns>
internal T CheckFeatureFlag<T>(string featureFlagKey, T defaultval)
{
// with testmode = true, the call goes through an interface so that we can intercept it with Mock
// with testmode = false, the call simply goes to the CheckFeatureFlagInternal
return testmode ? flags.CheckFeatureFlag(this, featureFlagKey, defaultval) : CheckFeatureFlagInternal(featureFlagKey, defaultval);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a performance thing I guess

}

private T CheckFeatureFlagInternal<T>(string featureFlagKey, T defaultval)
{
if(!(defaultval is bool || defaultval is string)){
throw new ArgumentException("unsupported flag type", defaultval.GetType().ToString());
Expand Down
3 changes: 3 additions & 0 deletions src/DynamoUtilities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
[assembly: InternalsVisibleTo("LibraryViewExtensionWebView2")]
[assembly: InternalsVisibleTo("Notifications")]
[assembly: InternalsVisibleTo("SystemTestServices")]
[assembly: InternalsVisibleTo("PackageManagerTests")]
//DynamicProxyGenAssembly2 is used by Mock to allow stubbing internal interfaces
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
74 changes: 73 additions & 1 deletion test/Libraries/PackageManagerTests/PackageLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -644,7 +649,74 @@ public void PlacingCustomNodeInstanceFromPackageRetainsCorrectPackageInfoState()
loader.RequestLoadNodeLibrary -= libraryLoader.LoadLibraryAndSuppressZTSearchImport;
}


[Test]
public void LoadPackagesInAssemblyIsolation()
{
var ff = new Mock<DynamoUtilities.IFFlags>();
DynamoModel.FeatureFlags.flags = ff.Object;
Copy link
Contributor

@aparajit-pratap aparajit-pratap Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make flags uppercase as per coding standards?

ff.Setup(x => x.CheckFeatureFlag<string>(DynamoModel.FeatureFlags, "IsolatePackages", "")).Returns(() => "Package1,Package2,Package");
ff.Setup(x => x.CheckFeatureFlag<string>(DynamoModel.FeatureFlags, "DoNotIsolatePackages", "")).Returns(() => "Package");

// 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 pathManager = new Mock<Dynamo.Interfaces.IPathManager>();
pathManager.SetupGet(x => x.PackagesDirectories).Returns(
() => new List<string> { PackagesDirectory });

var loader = new PackageLoader(pathManager.Object);
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()
{
Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions test/testAssemblyIsolation/Package1/pkg.json
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"]}
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions test/testAssemblyIsolation/Package2/pkg.json
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"]}
Loading
Loading