Skip to content

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

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:exc_finitypes
Jan 22, 2022
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:exc_finitypes

Conversation

@vstinner

@vstinner vstinner commented Jan 22, 2022 •

Copy link
Copy Markdown
Member

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().
Comment thread Objects/exceptions.c
return;
}

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

Copy link
Copy Markdown
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
Copy Markdown
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 ormsgpack/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 <emanuele.giaquinta@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants