Visitar URL original
On shutdown from Python release all slot holders by filmor · Pull Request #1720 · pythonnet/pythonnet · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
on runtime shutdown from Python release all slot holders, not only th…
…e ones, that belong to managed types
  • Loading branch information
lostmsu committed Mar 4, 2022
commit 5e041afadbb119bd2816bd483adf62a347b9c639
16 changes: 10 additions & 6 deletions src/runtime/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ internal static void Initialize()

internal static void RemoveTypes()
{
foreach (var type in cache.Values)
if (Runtime.HostedInPython)
{
if (Runtime.HostedInPython
&& _slotsHolders.TryGetValue(type, out var holder))
foreach (var holder in _slotsHolders)
{
// If refcount > 1, it needs to reset the managed slot,
// otherwise it can dealloc without any trick.
if (Runtime.Refcount(type) > 1)
if (holder.Key.Refcount > 1)
{
holder.ResetSlots();
holder.Value.ResetSlots();
}
}
}

foreach (var type in cache.Values)
{
type.Dispose();
}
cache.Clear();
Expand Down Expand Up @@ -507,7 +510,7 @@ internal static PyType CreateMetaType(Type impl, out SlotsHolder slotsHolder)
{
throw PythonException.ThrowLastAsClrException();
}

BorrowedReference dict = Util.ReadRef(type, TypeOffset.tp_dict);
using (var mod = Runtime.PyString_FromString("clr._internal"))
Runtime.PyDict_SetItemString(dict, "__module__", mod.Borrow());
Expand Down Expand Up @@ -726,6 +729,7 @@ internal static void CopySlot(BorrowedReference from, BorrowedReference to, int

internal static SlotsHolder CreateSlotsHolder(PyType type)
{
type = new PyType(type);
var holder = new SlotsHolder(type);
_slotsHolders.Add(type, holder);
return holder;
Expand Down
1 change: 0 additions & 1 deletion src/runtime/Types/ModuleObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ public static Assembly AddReference(string name)
/// <returns>The Type object</returns>

[ModuleFunction]
[ForbidPythonThreads]
public static Type GetClrType(Type type)
{
return type;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ def test_run_string():
assert sys.multiline_worked == 1

PythonEngine.ReleaseLock()

def test_leak_type():
import clr
sys._leaked_intptr = clr.GetClrType(System.IntPtr)