Commit f9c614b
committed
[Python] Don't exploit garbage collection hooks for memory regulator
Looking at the CPPInstance, I noticed that is claims to support cycling
garbage collection via the `Py_TPFLAGS_HAVE_GC`, but it doesn't stick
completely to the contract explained in the docs:
https://docs.python.org/3/c-api/gcsupport.html#c.PyObject_GC_Track
In particular, it breaks the constructor rules:
* The memory for the object must be allocated using PyObject_GC_New or
PyObject_GC_NewVar.
* Once all the fields which may contain references to other containers
are initialized, it must call PyObject_GC_Track().
The `tp_traverse` and `tp_clear` implementations actually do nothing
with the C Python API, so from the point of view of Python, there is no
reason to support cyclic garbage collection.
I think the CPPInstance tries to exploit the Python garbage collector to
trigger its own memory regulator code path early in `tp_clear`, but
breaking some of the Python C API contracts on the way. And the C++
object is also unregisterd from the memory regulator in `tp_dealloc`
anyway.
This might be the reason for the spurious failures in the CI, which
happen *between* two tests, hinting to garbage collector problems.
I think it's particularly problematic that the CPPInstance never
registers itself to the GC with `PyObject_GC_Track`, but then uses
`PyObject_GC_UnTrack`. That probably brings the garbage collector in a
bad state.
This reverts parts of the following CPyCppy commits from 2019:
* wlav/CPyCppyy@e43759a
* wlav/CPyCppyy@29eb42e1 parent 3ab1d5c commit f9c614b
1 file changed
+3
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
447 | 447 | | |
448 | 448 | | |
449 | 449 | | |
450 | | - | |
451 | 450 | | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
| 451 | + | |
464 | 452 | | |
465 | 453 | | |
466 | 454 | | |
| |||
1086 | 1074 | | |
1087 | 1075 | | |
1088 | 1076 | | |
1089 | | - | |
1090 | | - | |
| 1077 | + | |
1091 | 1078 | | |
1092 | 1079 | | |
1093 | | - | |
| 1080 | + | |
1094 | 1081 | | |
1095 | 1082 | | |
1096 | 1083 | | |
| |||
0 commit comments