Repository navigation
Commit 6d3443f
Fix a reference leak of a non-Struct base's type dict (#1199)
On Python 3.12+ `MS_GET_TYPE_DICT` expands to `PyType_GetDict`, which
returns a new reference; in the comment above the macro the result is
described as borrowed. At its only call site, in
`structmeta_collect_base`, there is no matching release on either exit,
so defining a `Struct` with a non-`Struct` base leaks one reference to
that base's type dict. Nothing holds that reference afterwards, so the
dict is never freed, and on 3.12 through 3.14 that keeps the base class
itself alive along with everything in its namespace, for the rest of the
process. `typing.Generic` counts as such a base, so generic `Struct`
definitions leak one too, in both the explicit and the PEP 695 spelling;
there the dict stays reachable regardless, so only the count grows.
Adds a release macro and applies it on both exits. It expands to
`Py_XDECREF` on 3.12+ and to nothing before that, where the `tp_dict`
slot is read directly and the reference is borrowed. Corrects the
comment to match. The remaining `tp_dict` uses in the file are direct
slot reads guarded against `NULL` and need no release.
A base that has not been readied yet, which a C extension can expose,
has no type dict, and on main the `__init__` and `__new__` check crashes
on such a base on every supported version. Such a base is now rejected
with `TypeError` before its type dict or inherited slots are read, and
`TypeError` is also raised if a readied base still has no type dict. The
check that the base is a type now comes first.
Adds a regression test that the base's contents are released when the
base passes the `__init__` and `__new__` check. On current main it fails
on 3.12, 3.13 and 3.14, and passes on 3.10 and 3.11, where the leak does
not exist. It also passes on main on 3.15, where the leaked dict no
longer keeps the base class reachable, so the class is collected and its
contents are released even though the dict itself is still retained.
Fixes #1198.
---------
Co-authored-by: Tseluiko Aleksandr <[email protected]>
Co-authored-by: sobolevn <[email protected]>1 parent 2f0b50d commit 6d3443f
2 files changed
Lines changed: 54 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
58 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
| 66 | + | |
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
| |||
5809 | 5813 | | |
5810 | 5814 | | |
5811 | 5815 | | |
| 5816 | + | |
| 5817 | + | |
| 5818 | + | |
| 5819 | + | |
| 5820 | + | |
| 5821 | + | |
| 5822 | + | |
| 5823 | + | |
5812 | 5824 | | |
5813 | 5825 | | |
| 5826 | + | |
| 5827 | + | |
| 5828 | + | |
| 5829 | + | |
| 5830 | + | |
| 5831 | + | |
| 5832 | + | |
| 5833 | + | |
| 5834 | + | |
5814 | 5835 | | |
5815 | 5836 | | |
5816 | 5837 | | |
| |||
5819 | 5840 | | |
5820 | 5841 | | |
5821 | 5842 | | |
5822 | | - | |
5823 | | - | |
5824 | | - | |
5825 | | - | |
5826 | | - | |
5827 | | - | |
5828 | | - | |
5829 | | - | |
5830 | 5843 | | |
5831 | 5844 | | |
5832 | 5845 | | |
| |||
5835 | 5848 | | |
5836 | 5849 | | |
5837 | 5850 | | |
| 5851 | + | |
| 5852 | + | |
| 5853 | + | |
| 5854 | + | |
| 5855 | + | |
| 5856 | + | |
| 5857 | + | |
| 5858 | + | |
5838 | 5859 | | |
5839 | 5860 | | |
5840 | 5861 | | |
| 5862 | + | |
5841 | 5863 | | |
5842 | 5864 | | |
5843 | 5865 | | |
| 5866 | + | |
5844 | 5867 | | |
5845 | 5868 | | |
5846 | 5869 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1009 | 1009 | | |
1010 | 1010 | | |
1011 | 1011 | | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
1012 | 1033 | | |
1013 | 1034 | | |
1014 | 1035 | | |
| |||
0 commit comments