gh-117657: Fix race involving immortalizing objects by colesbury · Pull Request #119927 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Include/internal/pycore_gc.h
4 changes: 2 additions & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ def suppress_immortalization(suppress=True):
yield
return

old_values = _testinternalcapi.set_immortalize_deferred(False)
_testinternalcapi.suppress_immortalization(True)
try:
yield
finally:
_testinternalcapi.set_immortalize_deferred(*old_values)
_testinternalcapi.suppress_immortalization(False)

def skip_if_suppress_immortalization():
try:
Expand Down
24 changes: 9 additions & 15 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1966,32 +1966,26 @@ get_py_thread_id(PyObject *self, PyObject *Py_UNUSED(ignored))
#endif

static PyObject *
set_immortalize_deferred(PyObject *self, PyObject *value)
suppress_immortalization(PyObject *self, PyObject *value)
{
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = PyInterpreterState_Get();
int old_enabled = interp->gc.immortalize.enabled;
int old_enabled_on_thread = interp->gc.immortalize.enable_on_thread_created;
int enabled_on_thread = 0;
if (!PyArg_ParseTuple(value, "i|i",
&interp->gc.immortalize.enabled,
&enabled_on_thread))
{
int suppress = PyObject_IsTrue(value);
if (suppress < 0) {
return NULL;
}
interp->gc.immortalize.enable_on_thread_created = enabled_on_thread;
return Py_BuildValue("ii", old_enabled, old_enabled_on_thread);
#else
return Py_BuildValue("OO", Py_False, Py_False);
PyInterpreterState *interp = PyInterpreterState_Get();
// Subtract two to suppress immortalization (so that 1 -> -1)
_Py_atomic_add_int(&interp->gc.immortalize, suppress ? -2 : 2);
#endif
Py_RETURN_NONE;
}

static PyObject *
get_immortalize_deferred(PyObject *self, PyObject *Py_UNUSED(ignored))
{
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = PyInterpreterState_Get();
return PyBool_FromLong(interp->gc.immortalize.enable_on_thread_created);
return PyBool_FromLong(_Py_atomic_load_int(&interp->gc.immortalize) >= 0);
#else
Py_RETURN_FALSE;
#endif
Expand Down Expand Up @@ -2111,7 +2105,7 @@ static PyMethodDef module_functions[] = {
#ifdef Py_GIL_DISABLED
{"py_thread_id", get_py_thread_id, METH_NOARGS},
#endif
{"set_immortalize_deferred", set_immortalize_deferred, METH_VARARGS},
{"suppress_immortalization", suppress_immortalization, METH_O},
{"get_immortalize_deferred", get_immortalize_deferred, METH_NOARGS},
#ifdef _Py_TIER2
{"uop_symbols_test", _Py_uop_symbols_test, METH_NOARGS},
Expand Down
4 changes: 2 additions & 2 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ should_intern_string(PyObject *o)
// unless we've disabled immortalizing objects that use deferred reference
// counting.
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->gc.immortalize.enable_on_thread_created) {
if (_Py_atomic_load_int(&interp->gc.immortalize) < 0) {
return 1;
}
#endif
Expand Down Expand Up @@ -240,7 +240,7 @@ intern_constants(PyObject *tuple, int *modified)
PyThreadState *tstate = PyThreadState_GET();
if (!_Py_IsImmortal(v) && !PyCode_Check(v) &&
!PyUnicode_CheckExact(v) &&
tstate->interp->gc.immortalize.enable_on_thread_created)
_Py_atomic_load_int(&tstate->interp->gc.immortalize) >= 0)
{
PyObject *interned = intern_one_constant(v);
if (interned == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,7 @@ _PyObject_SetDeferredRefcount(PyObject *op)
assert(op->ob_ref_shared == 0);
_PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED);
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->gc.immortalize.enabled) {
if (_Py_atomic_load_int_relaxed(&interp->gc.immortalize) == 1) {
// gh-117696: immortalize objects instead of using deferred reference
// counting for now.
_Py_SetImmortal(op);
Expand Down
6 changes: 3 additions & 3 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,15 +870,15 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
// gh-118527: Disable immortalization of code constants for explicit
// compile() calls to get consistent frozen outputs between the default
// and free-threaded builds.
// Subtract two to suppress immortalization (so that 1 -> -1)
PyInterpreterState *interp = _PyInterpreterState_GET();
int old_value = interp->gc.immortalize.enable_on_thread_created;
interp->gc.immortalize.enable_on_thread_created = 0;
_Py_atomic_add_int(&interp->gc.immortalize, -2);
#endif

result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);

#ifdef Py_GIL_DISABLED
interp->gc.immortalize.enable_on_thread_created = old_value;
_Py_atomic_add_int(&interp->gc.immortalize, 2);
#endif

Py_XDECREF(source_copy);
Expand Down
14 changes: 7 additions & 7 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,9 @@ _PyGC_Init(PyInterpreterState *interp)
{
GCState *gcstate = &interp->gc;

if (_Py_IsMainInterpreter(interp)) {
// gh-117783: immortalize objects that would use deferred refcounting
// once the first non-main thread is created.
gcstate->immortalize.enable_on_thread_created = 1;
}
// gh-117783: immortalize objects that would use deferred refcounting
// once the first non-main thread is created (but not in subinterpreters).
gcstate->immortalize = _Py_IsMainInterpreter(interp) ? 0 : -1;

gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) {
Expand Down Expand Up @@ -1808,8 +1806,10 @@ _PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp)
{
struct visitor_args args;
_PyEval_StopTheWorld(interp);
gc_visit_heaps(interp, &immortalize_visitor, &args);
interp->gc.immortalize.enabled = 1;
if (interp->gc.immortalize == 0) {
gc_visit_heaps(interp, &immortalize_visitor, &args);
interp->gc.immortalize = 1;
}
_PyEval_StartTheWorld(interp);
}

Expand Down
4 changes: 1 addition & 3 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1583,9 +1583,7 @@ new_threadstate(PyInterpreterState *interp, int whence)
}
else {
#ifdef Py_GIL_DISABLED
if (interp->gc.immortalize.enable_on_thread_created &&
!interp->gc.immortalize.enabled)
{
if (_Py_atomic_load_int(&interp->gc.immortalize) == 0) {
// Immortalize objects marked as using deferred reference counting
// the first time a non-main thread is created.
_PyGC_ImmortalizeDeferredObjects(interp);
Expand Down
2 changes: 0 additions & 2 deletions Tools/tsan/suppressions_free_threading.txt