8000 Fixed deadlock on finalizer after shutdown · pythonnet/pythonnet@1cb8e8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cb8e8c

Browse files
committed
Fixed deadlock on finalizer after shutdown
1 parent cc9e7e5 commit 1cb8e8c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/runtime/runtime.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ internal static void Initialize(bool initSigs = false, bool softShutdown = false
193193
}
194194
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
195195
}
196+
else
197+
{
198+
PyGILState_Ensure();
199+
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
200+
}
196201

197202
IsFinalizing = false;
198203

@@ -406,6 +411,16 @@ internal static void Shutdown(bool soft)
406411
PyGC_Collect();
407412
RuntimeState.Restore();
408413
ResetPyMembers();
414+
GC.Collect();
415+
try
416+
{
417+
GC.WaitForFullGCComplete();
418+
}
419+
catch (NotImplementedException)
420+
{
421+
// Some clr runtime didn't implement GC.WaitForFullGCComplete yet.
422+
}
423+
PyEval_SaveThread();
409424
return;
410425
}
411426
ResetPyMembers();

src/runtime/typemanager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,10 @@ private void ResetSlots()
10501050
return;
10511051
}
10521052
_alredyReset = true;
1053+
#if DEBUG
10531054
IntPtr tp_name = Marshal.ReadIntPtr(_type, TypeOffset.tp_name);
10541055
string typeName = Marshal.PtrToStringAnsi(tp_name);
1056+
#endif
10551057
foreach (var offset in _slots.Keys)
10561058
{
10571059
IntPtr ptr = GetDefaultSlot(offset);

0 commit comments

Comments
 (0)
0