Skip to content

Commit 927f980

Browse files
committed
fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown
#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 f506d65 commit 927f980

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/runtime/classderived.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ public static void Finalize(IPythonDerivedType obj)
857857
{
858858
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
859859
{
860-
self.gcHandle.Free();
860+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
861861
return;
862862
}
863863
}
@@ -872,7 +872,7 @@ public static void Finalize(IPythonDerivedType obj)
872872
// If python's been terminated then just free the gchandle.
873873
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
874874
{
875-
self.gcHandle.Free();
875+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
876876
return;
877877
}
878878

0 commit comments

Comments
 (0)