Skip to content

Commit

Permalink
Revert "when loading python engines dynamically fail fast (#14941)" (#…
Browse files Browse the repository at this point in the history
…14969)

This reverts commit d01a151.
  • Loading branch information
mjkkirschner authored Feb 24, 2024
1 parent d01a151 commit ed5376f
Show file tree
Hide file tree
Showing 7 changed files with 1,949 additions and 1,011 deletions.
17 changes: 2 additions & 15 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ Copyright © 2021 Alexe Zimarev

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

System.Collections.Immutable v.8.0.0:
The MIT License (MIT)
Copyright (c) .NET Foundation and Contributors
System.Collections.Immutable v.5.0.0:
Copyright (c) 2017 .NET Foundation and Contributors
All rights reserved.


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Expand Down Expand Up @@ -428,17 +426,6 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

System.Reflection.MetadataLoadContext v.8.0.0
The MIT License (MIT)
Copyright (c) .NET Foundation and Contributors
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

System.Numerics.Vectors v.4.5.0
The MIT License (MIT)
Copyright (c) .NET Foundation and Contributors
Expand Down
2,878 changes: 1,937 additions & 941 deletions doc/distrib/License.rtf

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<UILib>true</UILib>
</PropertyGroup>
Expand Down Expand Up @@ -158,7 +158,7 @@
<PackageReference Include="SharpDX.Direct3D9" Version="4.2.0" />
<PackageReference Include="SharpDX.DXGI" Version="4.2.0" />
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="6.0.0" />
<Reference Include="Dynamo.Microsoft.Xaml.Behaviors">
<HintPath>..\..\extern\Microsoft.Xaml.Behaviors\net6.0-windows\Dynamo.Microsoft.Xaml.Behaviors.dll</HintPath>
</Reference>
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoPackages/DynamoPackages.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageReference Include="Greg" Version="3.0.0.3175" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<NoWarn>MSB3539;CS1591;NUnit2005;NUnit2007;CS0618;CS0612;CS0672</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="System.Resources.Extensions" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions src/NodeServices/DynamoServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@
<PropertyGroup>
<NoWarn>MSB3539;CS1591;NUnit2005;NUnit2007;CS0618;CS0612;CS0672</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
</ItemGroup>
</Project>
54 changes: 6 additions & 48 deletions src/NodeServices/PythonServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Runtime;
Expand Down Expand Up @@ -39,9 +37,7 @@ namespace Dynamo.PythonServices
public enum EvaluationState { Success, Failed }

/// <summary>
/// This abstract class is intended to act as a base class for different python engines.
/// When you derive from this class to implement a new python engine, ensure all referenced assemblies from the
/// assembly that contains your python engine type are loadable at runtime or Dynamo will not load your python engine type.
/// This abstract class is intended to act as a base class for different python engines
/// </summary>
[SupressImportIntoVM]
[IsVisibleInDynamoLibrary(false)]
Expand Down Expand Up @@ -108,9 +104,6 @@ internal static readonly Lazy<PythonEngineManager>
new Lazy<PythonEngineManager>
(() => new PythonEngineManager());

private readonly string[] dotNetRuntimePaths;
private readonly IEnumerable<string> dynCorePaths;

#region Public members
/// <summary>
/// The actual instance stored in the Singleton class
Expand All @@ -136,6 +129,7 @@ internal static readonly Lazy<PythonEngineManager>
/// IronPython2 Engine name
/// </summary>
internal static readonly string IronPython2EngineName = "IronPython2";

internal static string PythonEvaluatorSingletonInstance = "Instance";

internal static string IronPythonEvaluatorClass = "IronPythonEvaluator";
Expand Down Expand Up @@ -170,8 +164,6 @@ private PythonEngineManager()
FirstOrDefault(a => a != null && a.GetName().Name == CPythonAssemblyName));

AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler((object sender, AssemblyLoadEventArgs args) => LoadDefaultPythonEngine(args.LoadedAssembly));
dotNetRuntimePaths = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");
dynCorePaths = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.GetFiles("*.dll", SearchOption.AllDirectories).Select(x => x.FullName);
}

private void LoadDefaultPythonEngine(Assembly a)
Expand Down Expand Up @@ -213,6 +205,7 @@ private void LoadPythonEngine(Assembly assembly)
{
return;
}

// Currently we are using try-catch to validate loaded assembly and Singleton Instance method exist
// but we can optimize by checking all loaded types against evaluators interface later
try
Expand All @@ -227,62 +220,27 @@ private void LoadPythonEngine(Assembly assembly)
instanceProp = eType?.GetProperty(PythonEvaluatorSingletonInstance, BindingFlags.NonPublic | BindingFlags.Static);
if (instanceProp == null) return;
}
catch
{
catch {
// Ignore exceptions from iterating assembly types.
return;
}

PythonEngine engine = (PythonEngine)instanceProp.GetValue(null);
if (engine == null)
if (engine == null)
{
throw new Exception($"Could not get a valid PythonEngine instance by calling the {eType.Name}.{PythonEvaluatorSingletonInstance} method");
}

VerifyEngineReferences(assembly,dotNetRuntimePaths.Concat(dynCorePaths));

if (GetEngine(engine.Name) == null)
{
AvailableEngines.Add(engine);
}
}
catch (Exception ex)
catch(Exception ex)
{
throw new Exception($"Failed to add a Python engine from assembly {assembly.GetName().Name}.dll with error: {ex.Message}");
}
}
/// <summary>
/// Attempts to verify that the dependencies of the given assembly can be found and loaded into an MetadataLoadContext.
/// Will throw exceptions if assemblies cannot be loaded.
/// Used to avoid loading python engines that can only be partially loaded and will fail later at a worse time.
/// </summary>
/// <param name="assembly">assembly to check references</param>
/// <param name="standardPaths">standard set of assembly paths that will be used to resolve the assemblt references</param>
private static void VerifyEngineReferences(Assembly assembly, IEnumerable<string> standardPaths)
{
MetadataLoadContext mlc = null;
//get assembly paths in the same directory tree as the assembly we're verifying.
var localAssemPaths = new FileInfo(assembly.Location).Directory.GetFiles("*.dll", SearchOption.AllDirectories).Select(x => x.FullName);

// Create PathAssemblyResolver that can resolve assemblies using the list of paths.
var resolver = new PathAssemblyResolver(standardPaths.Concat(localAssemPaths));
mlc = new MetadataLoadContext(resolver);

//at this point we have found an assembly that contains a python engine
//and is correctly formed, let's see if we can load its dependencies in the mlc
//this will help test that it will not fail to load later outside of a try / catch for example.
try
{
foreach (var asm in assembly.GetReferencedAssemblies())
{
var mlcasm = mlc.LoadFromAssemblyName(asm);
}
}
finally
{
mlc?.Dispose();
}
}
}

/// <summary>
Expand Down

0 comments on commit ed5376f

Please sign in to comment.