Skip to content

Finalizer Fixes #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pythonnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def unload():
global _RUNTIME
if _LOADER_ASSEMBLY is not None:
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"]
if func(b"") != 0:
if func(b"full_shutdown") != 0:
raise RuntimeError("Failed to call Python.NET shutdown")

if _RUNTIME is not None:
Expand Down
14 changes: 5 additions & 9 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,16 @@ public static IntPtr tp_repr(IntPtr ob)
public static void tp_dealloc(IntPtr ob)
{
ManagedType self = GetManagedObject(ob);
tp_clear(ob);
Runtime.PyObject_GC_UnTrack(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
RemoveObjectDict(ob);
Runtime.Py_CLEAR(ref self.tpHandle);
Runtime.PyObject_GC_UnTrack(ob);
Runtime.PyObject_GC_Del(ob);
self.FreeGCHandle();
}

public static int tp_clear(IntPtr ob)
{
ManagedType self = GetManagedObject(ob);
if (!self.IsTypeObject())
{
ClearObjectDict(ob);
}
self.tpHandle = IntPtr.Zero;
ClearObjectDict(ob);
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Linq;
using System.Diagnostics;

namespace Python.Runtime
{
Expand Down Expand Up @@ -270,7 +271,7 @@ private static void InitClassBase(Type type, ClassBase impl)

// Finally, initialize the class __dict__ and return the object.
var dict = new BorrowedReference(Marshal.ReadIntPtr(tp, TypeOffset.tp_dict));

Debug.Assert(!dict.IsNull);

if (impl.dotNetMembers == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void SetupGc ()
/// </summary>
public static void FinalizeObject(ManagedType self)
{
ClearObjectDict(self.pyHandle);
RemoveObjectDict(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
// Not necessary for decref of `tpHandle`.
self.FreeGCHandle();
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ protected virtual void OnSave(InterDomainContext context) { }
protected virtual void OnLoad(InterDomainContext context) { }

protected static void ClearObjectDict(IntPtr ob)
{
IntPtr dict = GetObjectDict(ob);
if (dict == IntPtr.Zero)
{
return;
}
Runtime.PyDict_Clear(dict);
}

protected static void RemoveObjectDict(IntPtr ob)
{
IntPtr dict = GetObjectDict(ob);
if (dict == IntPtr.Zero)
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ public static void tp_dealloc(IntPtr tp)
NativeCall.Void_Call_1(op, tp);
}

public static int tp_clear(IntPtr ob)
{
ClearObjectDict(ob);
return 0;
}

private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
{
var cb = GetManagedObject(tp) as ClassBase;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/methodobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static IntPtr tp_repr(IntPtr ob)
{
var self = (MethodObject)GetManagedObject(ob);
self.ClearMembers();
ClearObjectDict(ob);
RemoveObjectDict(ob);
self.Dealloc();
}

Expand Down
32 changes: 23 additions & 9 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,19 @@ private static void PyDictTryDelItem(BorrowedReference dict, string key)
private static void MoveClrInstancesOnwershipToPython()
{
var objs = ManagedType.GetManagedObjects();
var copyObjs = objs.ToArray();
foreach (var entry in copyObjs)
var copyObjs = new KeyValuePair<ManagedType, ManagedType.TrackTypes>[objs.Count];
{
ManagedType obj = entry.Key;
if (!objs.ContainsKey(obj))
int i = 0;
foreach (var entry in objs)
{
System.Diagnostics.Debug.Assert(obj.gcHandle == default);
continue;
ManagedType obj = entry.Key;
XIncref(obj.pyHandle);
copyObjs[i++] = entry;
}
}
foreach (var entry in copyObjs)
{
ManagedType obj = entry.Key;
if (entry.Value == ManagedType.TrackTypes.Extension)
{
obj.CallTypeClear();
Expand All @@ -522,11 +526,21 @@ private static void MoveClrInstancesOnwershipToPython()
PyObject_GC_Track(obj.pyHandle);
}
}
if (obj.gcHandle.IsAllocated)
}
foreach (var entry in copyObjs)
{
ManagedType obj = entry.Key;
if (!objs.ContainsKey(obj))
{
System.Diagnostics.Debug.Assert(obj.gcHandle == default);
continue;
}
if (obj.RefCount > 1)
{
obj.gcHandle.Free();
obj.FreeGCHandle();
Marshal.WriteIntPtr(obj.pyHandle, ObjectOffset.magic(obj.tpHandle), IntPtr.Zero);
}
obj.gcHandle = default;
XDecref(obj.pyHandle);
}
ManagedType.ClearTrackedObjects();
}
Expand Down