Skip to content

Commit ff170e9

Browse files
committed
Add Namespaces to the import hook only through AddReference
Don't piggyback on AssemblyManager's AssemblyLoadHandler method because it may be called from other threads, leading to deadlocks if it is called while Python code is executing
1 parent 059605b commit ff170e9

File tree

3 files changed

+9
-31
lines changed

3 files changed

+9
-31
lines changed

src/runtime/assemblymanager.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ internal class AssemblyManager
3737
// modified from event handlers below, potentially triggered from different .NET threads
3838
private static ConcurrentQueue<Assembly> assemblies;
3939
internal static List<string> pypath;
40-
41-
// Triggered when a new namespace is added to the namespaces dictionary
42-
// public static event Action<string> namespaceAdded;
43-
4440
private AssemblyManager()
4541
{
4642
}
@@ -287,17 +283,6 @@ internal static void ScanAssembly(Assembly assembly)
287283
if (ns != null)
288284
{
289285
namespaces[ns].TryAdd(assembly, string.Empty);
290-
// try
291-
// {
292-
// namespaceAdded?.Invoke(ns);
293-
// }
294-
// catch (Exception e)
295-
// {
296-
// // For some reason, exceptions happening here does... nothing.
297-
// // Even System.AccessViolationExceptions gets ignored.
298-
// Console.WriteLine($"Namespace added callback failed with: {e}");
299-
// throw;
300-
// }
301286
}
302287

303288
if (ns != null && t.IsGenericTypeDefinition)

src/runtime/importhook.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,21 @@ static void SetupNamespaceTracking()
176176
newset.Dispose();
177177
}
178178

179-
// AssemblyManager.namespaceAdded += OnNamespaceAdded;
180-
// PythonEngine.AddShutdownHandler(() => AssemblyManager.namespaceAdded -= OnNamespaceAdded);
181179
}
182180

183181
/// <summary>
184-
/// Removes the set of available namespaces from the clr module and
185-
/// removes the callback on the OnNamespaceAdded event.
182+
/// Removes the set of available namespaces from the clr module.
186183
/// </summary>
187184
static void TeardownNameSpaceTracking()
188185
{
189-
// AssemblyManager.namespaceAdded -= OnNamespaceAdded;
190186
// If the C# runtime isn't loaded, then there are no namespaces available
191187
Runtime.PyDict_SetItemString(root.dict, availableNsKey, Runtime.PyNone);
192188
}
193189

194-
public static void OnNamespaceAdded(string name)
190+
public static void AddNamespace(string name)
195191
{
196-
Console.WriteLine(System.Environment.StackTrace);
197-
Console.WriteLine("OnNamespaceAdded: acquiring");
198-
Console.Out.Flush();
199192
using (Py.GIL())
200193
{
201-
Console.WriteLine("OnNamespaceAdded: acquired");
202-
Console.Out.Flush();
203194
var pyNs = Runtime.PyString_FromString(name);
204195
try
205196
{
@@ -217,8 +208,6 @@ public static void OnNamespaceAdded(string name)
217208
Runtime.XDecref(pyNs);
218209
}
219210
}
220-
Console.WriteLine("OnNamespaceAdded: released");
221-
Console.Out.Flush();
222211
}
223212

224213

src/runtime/moduleobject.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ public static bool SuppressOverloads
509509
public static Assembly AddReference(string name)
510510
{
511511
AssemblyManager.UpdatePath();
512+
var origNs = AssemblyManager.GetNamespaces();
512513
Assembly assembly = null;
513514
assembly = AssemblyManager.FindLoadedAssembly(name);
514515
if (assembly == null)
@@ -530,9 +531,12 @@ public static Assembly AddReference(string name)
530531
// Classes that are not in a namespace needs an extra nudge to be found.
531532
ImportHook.UpdateCLRModuleDict();
532533

533-
// Heavyhanded but otherwise we'd need a "addedSinceLastCall".
534-
foreach(var ns in AssemblyManager.GetNamespaces()){
535-
ImportHook.OnNamespaceAdded(ns);
534+
// A bit heavyhanded, but we can't use the AssemblyManager's AssemblyLoadHandler
535+
// method because it may be called from other threads, leading to deadlocks
536+
// if it is called while Python code is executing.
537+
var currNs = AssemblyManager.GetNamespaces().Except(origNs);
538+
foreach(var ns in currNs){
539+
ImportHook.AddNamespace(ns);
536540
}
537541
return assembly;
538542
}

0 commit comments

Comments
 (0)