Skip to content

Commit e2ab3ae

Browse files
authored
fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown (#1260)
#1256 #1256 During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation. The fix is to check if the link has already been severed and skip that step during finalization.
1 parent 7e73b0d commit e2ab3ae

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/runtime/classderived.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ public static void Finalize(IPythonDerivedType obj)
858858
{
859859
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
860860
{
861-
self.gcHandle.Free();
861+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
862862
return;
863863
}
864864
}
@@ -873,7 +873,7 @@ public static void Finalize(IPythonDerivedType obj)
873873
// If python's been terminated then just free the gchandle.
874874
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
875875
{
876-
self.gcHandle.Free();
876+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
877877
return;
878878
}
879879

0 commit comments

Comments
 (0)