Skip to content

Commit 4e5afdf

Browse files
committed
Fix access violation exception on shutdown (#1977)
When nulling the GC handles on shutdown the reference count of all objects pointed to by the IntPtr in the `CLRObject.reflectedObjects` are zero. This caused an exception in some scenarios because `Runtime.PyObject_TYPE(reflectedClrObject)` is called while the reference counter is at zero. After `TypeManager.RemoveTypes();` is called in the `Runtime.Shutdown()` method, reference count decrements to zero do not invoke `ClassBase.tp_clear` for managed objects anymore which normally is responsible for removing references from `CLRObject.reflectedObjects`. Collecting objects referenced in `CLRObject.reflectedObjects` only after leads to an unstable state in which the reference count for these object addresses is zero while still maintaining them to be used for further pseudo-cleanup. In that time, the memory could have been reclaimed already which leads to the exception.
1 parent b112885 commit 4e5afdf

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- Dmitriy Se ([@dmitriyse](https://github.com/dmitriyse))
3939
- Félix Bourbonnais ([@BadSingleton](https://github.com/BadSingleton))
4040
- Florian Treurniet ([@ftreurni](https://github.com/ftreurni))
41+
- Frank Witscher ([@Frawak](https://github.com/Frawak))
4142
- He-chien Tsai ([@t3476](https://github.com/t3476))
4243
- Inna Wiesel ([@inna-w](https://github.com/inna-w))
4344
- Ivan Cronyn ([@cronan](https://github.com/cronan))

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2323

2424
- Fixed RecursionError for reverse operators on C# operable types from python. See #2240
2525
- Fixed probing for assemblies in `sys.path` failing when a path in `sys.path` has invalid characters. See #2376
26+
- Fixed possible access violation exception on shutdown. See ([#1977][i1977])
2627

2728
## [3.0.3](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.3) - 2023-10-11
2829

@@ -970,3 +971,4 @@ This version improves performance on benchmarks significantly compared to 2.3.
970971
[i1481]: https://github.com/pythonnet/pythonnet/issues/1481
971972
[i1672]: https://github.com/pythonnet/pythonnet/pull/1672
972973
[i2311]: https://github.com/pythonnet/pythonnet/issues/2311
974+
[i1977]: https://github.com/pythonnet/pythonnet/issues/1977

src/runtime/Runtime.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ internal static void Shutdown()
278278
ClearClrModules();
279279
RemoveClrRootModule();
280280

281+
TryCollectingGarbage(MaxCollectRetriesOnShutdown, forceBreakLoops: true);
282+
281283
NullGCHandles(ExtensionType.loadedExtensions);
282284
ClassManager.RemoveClasses();
283285
TypeManager.RemoveTypes();
@@ -295,8 +297,7 @@ internal static void Shutdown()
295297
PyObjectConversions.Reset();
296298

297299
PyGC_Collect();
298-
bool everythingSeemsCollected = TryCollectingGarbage(MaxCollectRetriesOnShutdown,
299-
forceBreakLoops: true);
300+
bool everythingSeemsCollected = TryCollectingGarbage(MaxCollectRetriesOnShutdown);
300301
Debug.Assert(everythingSeemsCollected);
301302

302303
Finalizer.Shutdown();

0 commit comments

Comments
 (0)