Reference counting bug with manually allocated heap types #128923
Labels
3.14
new features, bugs and security fixes
topic-free-threading
type-bug
An unexpected behavior, bug, or error
type-crash
A hard crash of the interpreter, possibly with a core dump
Bug report
Found by @vfdev-5.
This is specific to the free threading build and 3.14.
XLA/Jax uses the following code to create a heap type:
https://github.com/openxla/xla/blob/19a8e8e05fb34c5c4b8c38c9a8225e89f008c8c1/xla/python/pmap_lib.cc#L1027-L1058
In other words, the heap type is created by by calling
PyType_Type.tp_alloc
and filling in the fields, instead of the more common use ofPyType_FromSpec
. This leavesunique_id
zero initialized. The problem is thatunique_id=0
currently looks like a valid unique id for per-thread reference counting, which leads to reference counting errors and use-after-frees.I think we should change the per-thread reference counting so that
unique_id=0
is the sentinel value indicating that it's not assigned instead of the currentunique_id=-1
convention.Full repro
Linked PRs
__module__
#128951The text was updated successfully, but these errors were encountered: