Repository navigation
Check for ref counting bugs in debug mode caused by immortal objects. #94851
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error3.12only security fixesonly security fixes
on Jul 14, 2022 Hmm FYI on macOS latest branch: 6cbb57f
➜ cpython git:(main) ✗ ./python.exe -I -X showrefcount -c pass [0 refs, 0 blocks]It's not reproducible but I didn't check it on Linux.
Same on Ubuntu 20.04.4 LTS 6cbb57f
corona10@python-dev:~/cpython$ ./python -I -X showrefcount -c pass [0 refs, 0 blocks]I tested on
Ubuntu 20.04.4 LTSwith5.4.0-1074-azurekernel.I think it is dependent on which code path is executed by site module but it doesn't matter as c functions always returns strong references and never borrowed references.
@ericsnowcurrently
IIUC, once the https://peps.python.org/pep-0683/ is landed, we do not have to handle these kinds of reference counting operations for _Py_STR cases. Is it correct?IIUC, once the https://peps.python.org/pep-0683/ is landed, we do not have to handle these kinds of reference counting operations for _Py_STR cases. Is it correct?
Yes, once immortal objects are implemented then we can take advantage of this work and remove all the reference counting, even automate it once we have all the correct reference counting of immortal objects.
Also this issue would be relevant even after immortal objects as even with immortal objects we still want stable
-X showrefcount.FTR, so far the following PRs have been merged which fixed refcounting on immortal objects:
@ericsnowcurrently IIUC, once the https://peps.python.org/pep-0683/ is landed, we do not have to handle these kinds of reference counting operations for _Py_STR cases. Is it correct?
Right.
I suspect we'll drop this check later. However, it is helpful until then.
Fixed by #95001
- added a commit that references this issue
on Mar 14, 2023 - added a commit that references this issue
on Mar 14, 2023 - added a commit that references this issue
on Apr 11, 2023
With #90699, the identifiers are statically allocated and are immortal. This makes it easy to make reference counting mistakes as they are not detected and cause negative ref count in
_Py_RefTotal.On my machine the reference count is negative because of missing incref on
&_Py_STR(empty):PR #94850 fixes this issue.
To make it easy to discover reference counting issue, I propose to after each runtime finalization check that all the static allocated immortal objects have ref count of
999999999otherwise_PyObject_Dumpcan be used to output the object and abort the process in debug mode and this will help prevent these kinds of issues of "unstable" ref count.cc @ericsnowcurrently
Linked PRs