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
34 changes: 34 additions & 0 deletions src/DynamoCore/Extensions/ExtensionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,40 @@ private void Log(ILogMessage obj)
}
}

/// <summary>
/// Get the path to the extension/viewExtension assembly
/// </summary>
/// <param name="xmlPath">The path to the extension/viewExtension xml</param>
/// <returns></returns>
internal static string GetExtensionPath(string xmlPath)
{
var document = new XmlDocument();
document.Load(xmlPath);

var topNode = document.GetElementsByTagName("ViewExtensionDefinition");
if (topNode.Count == 0)
{
topNode = document.GetElementsByTagName("ExtensionDefinition");
}

if (topNode.Count == 0)
{
throw new Exception("Could not find any ExtensionDefinition or ViewExtensionDefinition top node.");
}

string extensionPath = string.Empty;
foreach (XmlNode item in topNode[0].ChildNodes)
{
if (item.Name == "AssemblyPath")
{
extensionPath = Path.Combine(new DirectoryInfo(xmlPath).Parent.FullName, item.InnerText);
break;
}
}

return extensionPath;
}

private void Log(string msg)
{
Log(LogMessage.Info(msg));
Expand Down
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 @@ -2202,7 +2203,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
54 changes: 51 additions & 3 deletions src/DynamoPackages/Package.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
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 All @@ -14,6 +16,7 @@
using Dynamo.Properties;
using Dynamo.Utilities;
using Greg.Requests;
using Greg.Responses;
using Newtonsoft.Json;
using String = System.String;

Expand Down Expand Up @@ -351,7 +354,7 @@ internal void AddAssemblies(IEnumerable<PackageAssembly> assems)
/// I.E. Builtin Packages.
/// </summary>
/// <returns>The list of all node library assemblies</returns>
internal IEnumerable<PackageAssembly> EnumerateAndLoadAssembliesInBinDirectory()
internal IEnumerable<PackageAssembly> EnumerateAndLoadAssembliesInBinDirectory(AssemblyLoadContext alc)
{
var assemblies = new List<PackageAssembly>();

Expand All @@ -374,7 +377,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(alc, assemFile.FullName, out assem);
if (result)
{
// IsNodeLibrary may fail, we store the warnings here and then show
Expand Down Expand Up @@ -572,21 +575,66 @@ internal void SetAsLoaded()
RaisePropertyChanged(nameof(LoadState));
}

AssemblyLoadContext GetPackageALC()
{
if (LoadedAssemblies.Any())
{
if (LoadedAssemblies.All(x => AssemblyLoadContext.GetLoadContext(x.Assembly) != AssemblyLoadContext.Default))
{
return AssemblyLoadContext.GetLoadContext(LoadedAssemblies[0].Assembly);
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return AssemblyLoadContext.Default;
}



internal void UninstallCore(CustomNodeManager customNodeManager, PackageLoader packageLoader, IPreferences prefs)
{
if (LoadedAssemblies.Any())
{
bool unloadAnyPackage = DynamoModel.FeatureFlags?.CheckFeatureFlag("UnloadAnyIsolatedPackage", false) ?? false;
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
bool unloadThisPackage = DynamoModel.FeatureFlags?.CheckFeatureFlag("UnloadIsolatedPackage_" + this.Name, false) ?? false;
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved

if (unloadAnyPackage || unloadThisPackage)
{
var alc = GetPackageALC();
if (alc != AssemblyLoadContext.Default)
{
void Alc_Unloading(AssemblyLoadContext obj)
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
{
alc.Unloading -= Alc_Unloading;
try
{
RemovePackage(customNodeManager, packageLoader, prefs);
}
catch
{
// Exception already logged inside RemovePackage
}
}

alc.Unloading += Alc_Unloading;
alc.Unload();
}
}

MarkForUninstall(prefs);
return;
}

RemovePackage(customNodeManager, packageLoader, prefs);
}

private void RemovePackage(CustomNodeManager customNodeManager, PackageLoader packageLoader, IPreferences prefs)
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
LoadedCustomNodes.ToList().ForEach(x => customNodeManager.Remove(x.FunctionId));
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
47 changes: 47 additions & 0 deletions src/DynamoPackages/PackageAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

namespace DynamoPackages
{
internal class PkgAssemblyLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;
public PkgAssemblyLoadContext(string name, string pluginPath, bool unloadable = true) : base(name, unloadable)
{
_resolver = new AssemblyDependencyResolver(pluginPath);

}

protected override Assembly Load(AssemblyName assemblyName)
{
var oldAss = Default.Assemblies.FirstOrDefault(x => x.GetName().Equals(assemblyName));
if (oldAss != null)
pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
{
// not sure about this. Hoping we can avoid loading assemblies that are alrady loaded in the default context.
return null;
}

string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
var newAss = LoadFromAssemblyPath(assemblyPath);
return newAss;
}

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

{
string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

return IntPtr.Zero;
}
}
}
46 changes: 41 additions & 5 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using Dynamo.Core;
using Dynamo.Exceptions;
using Dynamo.Extensions;
using Dynamo.Interfaces;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using DynamoPackages;
using DynamoPackages.Properties;
using DynamoUtilities;

Expand Down Expand Up @@ -200,9 +202,45 @@ private void TryLoadPackageIntoLibrary(Package package)
{
var dynamoVersion = VersionUtilities.PartialParse(DynamoModel.Version);

package.EnumerateAdditionalFiles();

// If the additional files contained an extension manifest, then request it be loaded.
var extensions = package.AdditionalFiles.Where(
file => file.Model.Name.Contains("ExtensionDefinition.xml") || file.Model.Name.Contains("ViewExtensionDefinition.xml")).ToList();

AssemblyLoadContext pkgLoadContext = AssemblyLoadContext.Default;

bool isolateAnyPackage = DynamoModel.FeatureFlags?.CheckFeatureFlag("IsolateAnyPackage", false) ?? false;
bool isolateThisPackage = DynamoModel.FeatureFlags?.CheckFeatureFlag("IsolatePackage_" + package.Name, false) ?? false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should check per version ?
probably would be helpful for IronPython which already has isolation. The current version should not be isolated by Dynamo probably...but all other could be...

if (isolateAnyPackage || isolateThisPackage)
{
if (extensions.Count() == 1)
{
var ext = extensions[0];
string entryPoint = string.Empty;
try
{
entryPoint = ExtensionLoader.GetExtensionPath(ext.Model.FullName);
if (!string.IsNullOrEmpty(entryPoint))
{
pkgLoadContext = new PkgAssemblyLoadContext(package.Name + "@" + package.VersionName, Path.Combine(package.RootDirectory, entryPoint));
}
}
catch (Exception ex)
{
Log(ex);
}
}
else if (package.Header.node_libraries.Any())
{
string entryPoint = new AssemblyName(package.Header.node_libraries.First()).Name + ".dll";
Copy link
Member

Choose a reason for hiding this comment

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

there could be more than one node_library right?

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'll try the root package location first...then I'll circle back to this if needed

pkgLoadContext = new PkgAssemblyLoadContext(package.Name + "@" + package.VersionName, Path.Combine(package.BinaryDirectory, entryPoint));
}
}

pinzart90 marked this conversation as resolved.
Show resolved Hide resolved
List<Assembly> blockedAssemblies = new List<Assembly>();
// Try to load node libraries from all assemblies
foreach (var assem in package.EnumerateAndLoadAssembliesInBinDirectory())
foreach (var assem in package.EnumerateAndLoadAssembliesInBinDirectory(pkgLoadContext))
{
if (assem.IsNodeLibrary)
{
Expand Down Expand Up @@ -250,8 +288,6 @@ private void TryLoadPackageIntoLibrary(Package package)
var customNodes = OnRequestLoadCustomNodeDirectory(package.CustomNodeDirectory, packageInfo);
package.LoadedCustomNodes.AddRange(customNodes);

package.EnumerateAdditionalFiles();

// If the additional files contained an extension manifest, then request it be loaded.
var extensionManifests = package.AdditionalFiles.Where(
file => file.Model.Name.Contains("ExtensionDefinition.xml") && !file.Model.Name.Contains("ViewExtensionDefinition.xml")).ToList();
Expand Down Expand Up @@ -751,11 +787,11 @@ internal static void CleanSharedPublishLoadContext(MetadataLoadContext mlc)
/// <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
Loading