Skip to content

Commit

Permalink
Fix crash with python 3.11 (#223)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
exg committed Jan 24, 2024
1 parent a959a36 commit 554831f
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ pub fn init_typerefs() {
DEFAULT = PyUnicode_InternFromString("default\0".as_ptr() as *const c_char);
EXT_HOOK = PyUnicode_InternFromString("ext_hook\0".as_ptr() as *const c_char);
OPTION = PyUnicode_InternFromString("option\0".as_ptr() as *const c_char);
Py_INCREF(PyExc_TypeError);
MsgpackEncodeError = PyExc_TypeError;
Py_INCREF(PyExc_ValueError);
MsgpackDecodeError = PyExc_ValueError;

HASH_BUILDER.get_or_init(ahash_init);
Expand Down

0 comments on commit 554831f

Please sign in to comment.