Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c07fff0

Browse files
author
Benoit Hudson
committed
UNI-62864: shutdown on domain reload.
Shut down python when the domain that loaded python reloads.
1 parent 410ac15 commit c07fff0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.CodeDom.Compiler;
33
using System.Reflection;
44
using NUnit.Framework;
5+
using Python.Runtime;
56

67
namespace Python.EmbeddingTest
78
{
@@ -48,6 +49,9 @@ public static void DomainReloadAndGC()
4849
Assembly pythonRunner1 = BuildAssembly("test1");
4950
RunAssemblyAndUnload(pythonRunner1, "test1");
5051

52+
// Verify that python is not initialized even though we ran it.
53+
Assert.That(Runtime.Runtime.Py_IsInitialized(), Is.Zero);
54+
5155
// This caused a crash because objects allocated in pythonRunner1
5256
// still existed in memory, but the code to do python GC on those
5357
// objects is gone.

src/runtime/pythonengine.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,17 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true)
220220
{
221221
locals.Dispose();
222222
}
223+
224+
// Make sure we clean up properly on app domain unload.
225+
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
223226
}
224227
}
225228

229+
static void OnDomainUnload(object _, EventArgs __)
230+
{
231+
Shutdown();
232+
}
233+
226234
/// <summary>
227235
/// A helper to perform initialization from the context of an active
228236
/// CPython interpreter process - this bootstraps the managed runtime
@@ -303,6 +311,8 @@ public static void Shutdown()
303311
_pythonPath = IntPtr.Zero;
304312

305313
Runtime.Shutdown();
314+
315+
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
306316
initialized = false;
307317
}
308318
}

0 commit comments

Comments
 (0)