Skip to content

Commit 5e041af

Browse files
committed
on runtime shutdown from Python release all slot holders, not only the ones, that belong to managed types
1 parent eec30c0 commit 5e041af

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/runtime/TypeManager.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ internal static void Initialize()
5151

5252
internal static void RemoveTypes()
5353
{
54-
foreach (var type in cache.Values)
54+
if (Runtime.HostedInPython)
5555
{
56-
if (Runtime.HostedInPython
57-
&& _slotsHolders.TryGetValue(type, out var holder))
56+
foreach (var holder in _slotsHolders)
5857
{
5958
// If refcount > 1, it needs to reset the managed slot,
6059
// otherwise it can dealloc without any trick.
61-
if (Runtime.Refcount(type) > 1)
60+
if (holder.Key.Refcount > 1)
6261
{
63-
holder.ResetSlots();
62+
holder.Value.ResetSlots();
6463
}
6564
}
65+
}
66+
67+
foreach (var type in cache.Values)
68+
{
6669
type.Dispose();
6770
}
6871
cache.Clear();
@@ -507,7 +510,7 @@ internal static PyType CreateMetaType(Type impl, out SlotsHolder slotsHolder)
507510
{
508511
throw PythonException.ThrowLastAsClrException();
509512
}
510-
513+
511514
BorrowedReference dict = Util.ReadRef(type, TypeOffset.tp_dict);
512515
using (var mod = Runtime.PyString_FromString("clr._internal"))
513516
Runtime.PyDict_SetItemString(dict, "__module__", mod.Borrow());
@@ -726,6 +729,7 @@ internal static void CopySlot(BorrowedReference from, BorrowedReference to, int
726729

727730
internal static SlotsHolder CreateSlotsHolder(PyType type)
728731
{
732+
type = new PyType(type);
729733
var holder = new SlotsHolder(type);
730734
_slotsHolders.Add(type, holder);
731735
return holder;

src/runtime/Types/ModuleObject.cs

-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ public static Assembly AddReference(string name)
542542
/// <returns>The Type object</returns>
543543

544544
[ModuleFunction]
545-
[ForbidPythonThreads]
546545
public static Type GetClrType(Type type)
547546
{
548547
return type;

tests/test_engine.py

+4
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ def test_run_string():
4141
assert sys.multiline_worked == 1
4242

4343
PythonEngine.ReleaseLock()
44+
45+
def test_leak_type():
46+
import clr
47+
sys._leaked_intptr = clr.GetClrType(System.IntPtr)

0 commit comments

Comments
 (0)