Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 2039e69

Browse files
committed
* Fix deadlock on domain unload
* Reset Runtime.Exceptions
1 parent 77da6df commit 2039e69

4 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/embed_tests/TestDomainReload.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public static void DomainReloadAndGC()
5656
Assembly pythonRunner1 = BuildAssembly("test1");
5757
RunAssemblyAndUnload(pythonRunner1, "test1");
5858

59-
// Verify that python is not initialized even though we ran it.
60-
//Assert.That(Runtime.Runtime.Py_IsInitialized(), Is.Zero);
59+
Assert.That(Runtime.Runtime.Py_IsInitialized() != 0,
60+
"On soft-shutdown mode, Python runtime should still running");
6161

6262
// This caused a crash because objects allocated in pythonRunner1
6363
// still existed in memory, but the code to do python GC on those
@@ -83,7 +83,7 @@ public static void RunPython() {
8383
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
8484
string name = AppDomain.CurrentDomain.FriendlyName;
8585
Console.WriteLine(string.Format(""[{0} in .NET] In PythonRunner.RunPython"", name));
86-
//PythonEngine.Initialize(softShutdown: true);
86+
PythonEngine.Initialize(softShutdown: true);
8787
using (Py.GIL()) {
8888
try {
8989
var pyScript = string.Format(""import clr\n""
@@ -99,6 +99,7 @@ public static void RunPython() {
9999
Console.WriteLine(string.Format(""[{0} in .NET] Caught exception: {1}"", name, e));
100100
}
101101
}
102+
PythonEngine.BeginAllowThreads();
102103
}
103104
static void OnDomainUnload(object sender, EventArgs e) {
104105
System.Console.WriteLine(string.Format(""[{0} in .NET] unloading"", AppDomain.CurrentDomain.FriendlyName));

src/runtime/exceptions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,21 @@ internal static void Initialize()
132132
/// </summary>
133133
internal static void Shutdown()
134134
{
135-
if (Runtime.Py_IsInitialized() != 0)
135+
if (Runtime.Py_IsInitialized() == 0)
136136
{
137-
Type type = typeof(Exceptions);
138-
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
137+
return;
138+
}
139+
Type type = typeof(Exceptions);
140+
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
141+
{
142+
var op = (IntPtr)fi.GetValue(type);
143+
if (op != IntPtr.Zero)
139144
{
140-
var op = (IntPtr)fi.GetValue(type);
141-
if (op != IntPtr.Zero)
142-
{
143-
Runtime.XDecref(op);
144-
}
145+
Runtime.XDecref(op);
145146
}
146-
Runtime.XDecref(exceptions_module);
147-
Runtime.PyObject_HasAttrString(warnings_module, "xx");
148-
Runtime.XDecref(warnings_module);
149147
}
148+
Runtime.Py_CLEAR(ref exceptions_module);
149+
Runtime.Py_CLEAR(ref warnings_module);
150150
}
151151

152152
/// <summary>

src/runtime/pythonengine.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,19 @@ public static void InitExt()
324324
/// </remarks>
325325
public static void Shutdown()
326326
{
327-
if (initialized)
327+
if (!initialized)
328328
{
329-
PyScopeManager.Global.Clear();
330-
331-
// If the shutdown handlers trigger a domain unload,
332-
// don't call shutdown again.
333-
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
329+
return;
330+
}
331+
// If the shutdown handlers trigger a domain unload,
332+
// don't call shutdown again.
333+
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
334334

335-
ExecuteShutdownHandlers();
335+
PyScopeManager.Global.Clear();
336+
ExecuteShutdownHandlers();
336337

337-
initialized = false;
338-
}
338+
initialized = false;
339+
339340
}
340341

341342
/// <summary>

src/runtime/runtime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ internal static void Shutdown(bool soft)
394394
{
395395
return;
396396
}
397+
PyGILState_Ensure();
398+
397399
AssemblyManager.Shutdown();
398400
Exceptions.Shutdown();
399401
ImportHook.Shutdown();

0 commit comments

Comments
 (0)