Skip to content

bpo-46417: Py_Finalize() clears static exceptioins #30805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2022
Merged

bpo-46417: Py_Finalize() clears static exceptioins #30805

merged 2 commits into from
Jan 22, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jan 22, 2022

The Py_Finalize() function now clears exceptions implemented as
static types.

Add _PyExc_FiniTypes() function, called by _PyExc_Fini().

https://bugs.python.org/issue46417

The Py_Finalize() function now clears exceptions implemented as
static types.

Add _PyExc_FiniTypes() function, called by _PyExc_Fini().
return;
}

for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
for (Py_ssize_t i = Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well well well. I would make the line more consistent, but I decided to merge my PR since I'm working on a looong serie of changes: see https://bugs.python.org/issue46417

The CPython C code base is far from being consistent and the PEP 7 is quite relaxed in terms of coding style.

@vstinner vstinner merged commit 621a45c into python:main Jan 22, 2022
@vstinner vstinner deleted the exc_finitypes branch January 22, 2022 21:31
exg added a commit to aviramha/ormsgpack that referenced this pull request Jan 25, 2024
We borrow a reference to the static exceptions TypeError and
ValueError and pass these objects to PyModule_AddObject, which steals
a reference. In Python 3.11 and newer, Py_Finalize() deallocates
static exceptions

python/cpython#30805

This results in a crash on termination, because _PyStaticType_Dealloc
indirectly calls _Py_Dealloc on the objects as the reference count
drops to zero. The issue does not occur in Python 3.12 and newer
because the objects are immortal.

Signed-off-by: Emanuele Giaquinta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants