You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b2d1b7a
Browse filesBrowse the repository at this point in the historyBrowse files
Reject gc=False on struct types with a weakref slot (#1207)
On Python 3.12 and later, releasing an instance of a struct type
declared with both `gc=False` and `weakref=True` goes wrong. On default
builds it reads memory outside the instance's allocation, and creating
one instance is enough, since the read happens even when no weak
reference was ever taken:
```python
import msgspec
class Ex(msgspec.Struct, gc=False, weakref=True):
x: int
Ex(1)
```
The process usually crashes. On free-threaded builds there is no
immediate crash: the instance is released from the wrong address, the
allocator's own state is corrupted, and the failure arrives later and
somewhere else.
Since 3.12 the `__weakref__` slot is not inside the object. It sits at a
fixed negative offset, in the pre-header that is allocated in front of
the instance. A `gc=False` type clears `Py_TPFLAGS_HAVE_GC` and releases
the instance with `PyObject_Free` on the object itself, so the block is
released from the wrong address. Outside free-threaded builds that
pre-header is also smaller, which leaves the slot before the start of
the allocation.
This rejects the combination at class creation, the way `gc=False` with
`dict=True` is already rejected. The check also covers the other routes
to it: `defstruct`, inheriting either option from a base class, and a
`gc=False` struct whose base is a plain mixin with `__slots__ =
("__weakref__",)`. The structs guide documents the combination as
unsupported, and why.
The restriction is unconditional, so it also applies to 3.10 and 3.11,
where the slot is inside the object and the combination works. That is
deliberate: the same class definition then behaves the same way on every
supported version, and an upgrade of Python cannot turn working code
into an error.
An earlier revision of this branch kept such a type GC enabled instead,
which also fixed the crash. The discussion below records why the
restriction was chosen over it.
---------
Co-authored-by: Tseluiko Aleksandr <[email protected]>
Co-authored-by: sobolevn <[email protected]>
0 commit comments