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

use ALC for deps #1

Merged
merged 1 commit into from
Dec 12, 2023
Merged

use ALC for deps #1

merged 1 commit into from
Dec 12, 2023

Conversation

mjkkirschner
Copy link
Member

@mjkkirschner mjkkirschner commented Dec 8, 2023

I will likely move these change to the DSIronPython3 package instead, but adding this for feedback since the changes will look exactly the same there.

This requires one change on the Dynamo side that I need to check with @pinzart90 about.
Screenshot 2023-12-08 at 12 57 13 PM

personally I prefer the solution in this repo because it means we don't risk making any changes to Dynamo before release, and we instead can update at any time by updating the python engine packages.

The two important parts of this PR are:

  1. use an ALC to load the DSIronPython engine and its dependencies.
  2. move all the package binaries into the /extra folder to avoid having the package manager blindly load everything into the default ALC - instead we use the extension to create and load our deps into our own ALC.

Copy link

@aparajit-pratap aparajit-pratap left a comment

Choose a reason for hiding this comment

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

@mjkkirschner are you saying that you want to move this logic to the IronPython3 package so that this IronPython2 package can load in the default assembly context while the former loads isolated in a new ALC?

@mjkkirschner
Copy link
Member Author

mjkkirschner commented Dec 11, 2023

@mjkkirschner are you saying that you want to move this logic to the IronPython3 package so that this IronPython2 package can load in the default assembly context while the former loads isolated in a new ALC?

Thats what I was thinking - my logic was that IronPython2 is used widely and IronPython3 is not. Using the default load context is closer to what was occurring in Dynamo 2.x so theres less chance of unanticipated changes - BUT because we can release these updates to the packages at any time I feel it's not critical.

Alternatively, as an experiment it might be nice to isolate both packages and see what happens with actual users, then if we find problems we can release new versions. This could be a good first test of isolating ALL packages.

What do you think?

}
var extraPath = Path.Combine(new FileInfo(Assembly.GetAssembly(typeof(IronPythonExtension)).Location).Directory.Parent.FullName, "extra");
var alc = new IsolatedPythoContext(Path.Combine(extraPath,"DSIronPython.dll"));
alc.LoadFromAssemblyName(new AssemblyName("DSIronPython"));

Choose a reason for hiding this comment

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

this means that only the DSIronPython.dll assembly (and its own dependencies) will be isolated
The IronPythonExtension assembly will be part of Dynamo's ALC

This sounds fine to me. I did the same thing with Dynamo4Revit and it worked ok
As long as the IronPythonExtension 's dependencies are ok to be in Dynamo's ALC

Choose a reason for hiding this comment

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

Also I do not remember how the dependencies of the entrypoint (DSIronPython.dll) are resolved.
Hopefully it happens magically by using the ALC resolver and the deps.json file

I remember there were some issues with the dependencies that required special Resolver code in the Host app (that would be Dynamo in this case)

Choose a reason for hiding this comment

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

Please double check how the dependencies of DSIronPython.dll are resolved and in which ALC

Copy link
Member Author

@mjkkirschner mjkkirschner Dec 11, 2023

Choose a reason for hiding this comment

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

yes, correct IronPythonExtension basically has no dependencies except for Dynamo and at least my testing indicates that the assemblies are isolated from the default ALC (since ironPython3 (not isolated) works side by side))

I think I can double check by using The GetAssemblyLoadContext() method on each assembly, or maybe using dotnet-trace tooling see exactly when the load occurs.

Any other ideas for how to test Please double check how the dependencies of DSIronPython.dll are resolved and in which ALC besides the ideas above?

Choose a reason for hiding this comment

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

Harder to do, but you can simply put breakpoints in all the resolvers in Dynamo and in this extension in the Assembly Load(AssemblyName assemblyName) and implement https://learn.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolving?view=net-8.0
You could implement that in the dynamo default ALC and put a breakpoint too

Copy link
Member Author

@mjkkirschner mjkkirschner Dec 11, 2023

Choose a reason for hiding this comment

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

Here is the algorithm, we use the name variant:
https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/loading-managed#algorithm

I'm pretty sure that dependencies of the assembly that are loaded using by name - are loaded into the same ALC, or at least as indicated by the algorithm, an attempt is made to resolve them before falling back to the default ALC.

Image shows the dependencies loaded side by side

Screenshot 2023-12-11 at 5 19 45 PM

image shows the assemblies loaded into the python2.xALC

Screenshot 2023-12-11 at 5 26 15 PM

I checked the above with the deps.json files in the DSIronPython folder, and without them and got the same functioning behavior of side by side iron python versions and the same binaries loaded into the ALC.

Choose a reason for hiding this comment

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

This was the issue I was thinking of dotnet/runtime#93780 (comment)
But this does not seem to be the case with IronPython deps. They probably are all in the resolver folder

Copy link

@pinzart90 pinzart90 left a comment

Choose a reason for hiding this comment

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

LGTM

@mjkkirschner mjkkirschner merged commit 53e979c into DynamoDS:master Dec 12, 2023
9 of 10 checks passed
@mjkkirschner mjkkirschner deleted the isolate branch December 12, 2023 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants