Skip to content

Support pythonnet for AppDomain #78

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 20 additions & 5 deletions netfx_loader/ClrLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using NXPorts.Attributes;

Expand All @@ -21,23 +22,37 @@ public static void Initialize()
}
}

private static string AssemblyDirectory
{
get
{
// This is needed in case the DLL was shadow-copied
// (Otherwise .Location would work)
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}

[DllExport("pyclr_create_appdomain", CallingConvention.Cdecl)]
public static IntPtr CreateAppDomain(
[MarshalAs(UnmanagedType.LPUTF8Str)] string name,
[MarshalAs(UnmanagedType.LPUTF8Str)] string configFile
)
{
Print($"Creating AppDomain {name} with {configFile}");

var clrLoaderDir = AssemblyDirectory;
if (!string.IsNullOrEmpty(name))
{
var setup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
ApplicationBase = clrLoaderDir,
ConfigurationFile = configFile
};
Print($"Base: {AppDomain.CurrentDomain.BaseDirectory}");
Print($"Base: {clrLoaderDir}");
var domain = AppDomain.CreateDomain(name, null, setup);

Print($"Located domain {domain}");

var domainData = new DomainData(domain);
Expand All @@ -61,8 +76,8 @@ public static IntPtr GetFunction(
try
{
var domainData = _domains[(int)domain];
var deleg = domainData.GetEntryPoint(assemblyPath, typeName, function);
return Marshal.GetFunctionPointerForDelegate(deleg);
Print($"Getting functor for function {function} of type {typeName} in assembly {assemblyPath}");
return domainData.GetFunctor(assemblyPath, typeName, function);
}
catch (Exception exc)
{
Expand Down
99 changes: 80 additions & 19 deletions netfx_loader/DomainData.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,115 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;

namespace ClrLoader
{
using static ClrLoader;

class DomainData : IDisposable
public static class DomainSetup
{
public delegate int EntryPoint(IntPtr buffer, int size);

public static void StoreFunctorFromDomainData()
{
var domain = AppDomain.CurrentDomain;
var assemblyPath = (string)domain.GetData("_assemblyPath");
var typeName = (string)domain.GetData("_typeName");
var function = (string)domain.GetData("_function");
var deleg = GetDelegate(domain, assemblyPath, typeName, function);
var functor = Marshal.GetFunctionPointerForDelegate(deleg);
domain.SetData("_thisDelegate", deleg);
domain.SetData("_thisFunctor", functor);
}

private static Delegate GetDelegate(AppDomain domain, string assemblyPath, string typeName, string function)
{
var assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
var assembly = domain.Load(assemblyName);
var type = assembly.GetType(typeName, throwOnError: true);
var deleg = Delegate.CreateDelegate(typeof(EntryPoint), type, function);
return deleg;
}
}

class DomainData : IDisposable
{
bool _disposed = false;

public AppDomain Domain { get; }
public Dictionary<(string, string, string), EntryPoint> _delegates;
public Dictionary<(string, string, string), IntPtr> _functors;
public HashSet<string> _resolvedAssemblies;

public DomainData(AppDomain domain)
{
Domain = domain;
_delegates = new Dictionary<(string, string, string), EntryPoint>();
_functors = new Dictionary<(string, string, string), IntPtr>();
_resolvedAssemblies = new HashSet<string>();
}

public EntryPoint GetEntryPoint(string assemblyPath, string typeName, string function)
private void installResolver(string assemblyPath)
{
if (_disposed)
throw new InvalidOperationException("Domain is already disposed");

var key = (assemblyPath, typeName, function);

EntryPoint result;
var assemblyName = AssemblyName.GetAssemblyName(assemblyPath).Name;
if (_resolvedAssemblies.Contains(assemblyName))
return;
_resolvedAssemblies.Add(assemblyName);

if (!_delegates.TryGetValue(key, out result))
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var assembly = Domain.Load(AssemblyName.GetAssemblyName(assemblyPath));
var type = assembly.GetType(typeName, throwOnError: true);
if (args.Name.Contains(assemblyName))
return Assembly.LoadFrom(assemblyPath);
return null;
};
}

Print($"Loaded type {type}");
result = (EntryPoint)Delegate.CreateDelegate(typeof(EntryPoint), type, function);
private static readonly object _lockObj = new object();

_delegates[key] = result;
}
public IntPtr GetFunctor(string assemblyPath, string typeName, string function)
{
if (_disposed)
throw new InvalidOperationException("Domain is already disposed");

return result;
// neither the domain data nor the _functors dictionary is threadsafe
lock (_lockObj)
{
installResolver(assemblyPath);
var assemblyName = AssemblyName.GetAssemblyName(assemblyPath).Name;

var key = (assemblyName, typeName, function);

IntPtr result;
if (!_functors.TryGetValue(key, out result))
{
Domain.SetData("_assemblyPath", assemblyPath);
Domain.SetData("_typeName", typeName);
Domain.SetData("_function", function);

Domain.DoCallBack(new CrossAppDomainDelegate(DomainSetup.StoreFunctorFromDomainData));
result = (IntPtr)Domain.GetData("_thisFunctor");
if (result == IntPtr.Zero)
throw new Exception($"Unable to get functor for {assemblyName}, {typeName}, {function}");

// set inputs to StoreFunctorFromDomainData to null.
// (There's no method to explicitly clear domain data)
Domain.SetData("_assemblyPath", null);
Domain.SetData("_typeName", null);
Domain.SetData("_function", null);

// the result has to remain in the domain data because we don't know when the
// client of pyclr_get_function will actually invoke the functor, and if we
// remove it from the domain data after returning it may get collected too early.
_functors[key] = result;
}
return result;
}
}

public void Dispose()
{
if (!_disposed)
{
_delegates.Clear();
_functors.Clear();

if (Domain != AppDomain.CurrentDomain)
AppDomain.Unload(Domain);
Expand Down
Loading