Skip to content

Commit 2596cdf

Browse files
committed
fixed Python derived types crashing on shutdown of Python process
clearing GCHandle from an instance of Python derived type would drop the last reference to it, so it was destroyed without being removed from reflectedObjects collection
1 parent e9c3a3d commit 2596cdf

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/runtime/extensiontype.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,17 @@ public unsafe static void tp_dealloc(NewReference lastRef)
8080

8181
tp_clear(lastRef.Borrow());
8282

83-
bool deleted = loadedExtensions.Remove(lastRef.DangerousGetAddress());
84-
Debug.Assert(deleted);
85-
8683
// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
8784
DecrefTypeAndFree(lastRef.Steal());
8885
}
8986

9087
public static int tp_clear(BorrowedReference ob)
9188
{
92-
TryFreeGCHandle(ob);
89+
if (TryFreeGCHandle(ob))
90+
{
91+
bool deleted = loadedExtensions.Remove(ob.DangerousGetAddress());
92+
Debug.Assert(deleted);
93+
}
9394

9495
int res = ClassBase.BaseUnmanagedClear(ob);
9596
return res;

src/runtime/runtime.cs

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
339339
else if (forceBreakLoops)
340340
{
341341
NullGCHandles(CLRObject.reflectedObjects);
342+
CLRObject.reflectedObjects.Clear();
342343
}
343344
}
344345
return false;

0 commit comments

Comments
 (0)