From b45420da66e86d0bcc9c9614faeff70a02ebf0b5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 6 May 2020 17:14:47 +0200 Subject: [PATCH] bpo-40533: Make PyObject.ob_refcnt atomic in subinterpreters When Python is built with experimental isolated interpreters, declare PyObject.ob_refcnt and _dictkeysobject.dk_refcnt as atomic variables. Temporary workaround until subinterpreters stop sharing Python objects. --- Include/object.h | 7 +++++++ Objects/dict-common.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/Include/object.h b/Include/object.h index 6c30809124dea8..123e967ad6053b 100644 --- a/Include/object.h +++ b/Include/object.h @@ -104,7 +104,14 @@ typedef struct _typeobject PyTypeObject; */ typedef struct _object { _PyObject_HEAD_EXTRA +#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS + /* bpo-40533: Use an atomic variable for PyObject.ob_refcnt, to ensure that + Py_INCREF() and Py_DECREF() are safe when called in parallel, until + subinterpreters stop sharing Python objects. */ + _Atomic Py_ssize_t ob_refcnt; +#else Py_ssize_t ob_refcnt; +#endif PyTypeObject *ob_type; } PyObject; diff --git a/Objects/dict-common.h b/Objects/dict-common.h index 71d6b0274420b8..3383cd90248d2c 100644 --- a/Objects/dict-common.h +++ b/Objects/dict-common.h @@ -20,7 +20,13 @@ typedef Py_ssize_t (*dict_lookup_func) /* See dictobject.c for actual layout of DictKeysObject */ struct _dictkeysobject { +#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS + /* bpo-40533: Use an atomic variables until subinterpreters stop sharing + Python dictionaries. */ + _Atomic Py_ssize_t dk_refcnt; +#else Py_ssize_t dk_refcnt; +#endif /* Size of the hash table (dk_indices). It must be a power of 2. */ Py_ssize_t dk_size;