Skip to content

[3.13] gh-127020: Make PyCode_GetCode thread-safe for free threading (GH-127043) #127107

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 1 commit into from
Nov 21, 2024
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
30 changes: 30 additions & 0 deletions Lib/test/test_free_threading/test_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from threading import Thread
from unittest import TestCase

from test.support import threading_helper

@threading_helper.requires_working_threading()
class TestCode(TestCase):
def test_code_attrs(self):
"""Test concurrent accesses to lazily initialized code attributes"""
code_objects = []
for _ in range(1000):
code_objects.append(compile("a + b", "<string>", "eval"))

def run_in_thread():
for code in code_objects:
self.assertIsInstance(code.co_code, bytes)
self.assertIsInstance(code.co_freevars, tuple)
self.assertIsInstance(code.co_varnames, tuple)

threads = [Thread(target=run_in_thread) for _ in range(2)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a crash in the free threading build when :c:func:`PyCode_GetCode`,
:c:func:`PyCode_GetVarnames`, :c:func:`PyCode_GetCellvars`, or
:c:func:`PyCode_GetFreevars` were called from multiple threads at the same
time.
78 changes: 51 additions & 27 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,32 @@ validate_and_copy_tuple(PyObject *tup)
}

static int
init_co_cached(PyCodeObject *self) {
if (self->_co_cached == NULL) {
self->_co_cached = PyMem_New(_PyCoCached, 1);
if (self->_co_cached == NULL) {
init_co_cached(PyCodeObject *self)
{
_PyCoCached *cached = FT_ATOMIC_LOAD_PTR(self->_co_cached);
if (cached != NULL) {
return 0;
}

Py_BEGIN_CRITICAL_SECTION(self);
cached = self->_co_cached;
if (cached == NULL) {
cached = PyMem_New(_PyCoCached, 1);
if (cached == NULL) {
PyErr_NoMemory();
return -1;
}
self->_co_cached->_co_code = NULL;
self->_co_cached->_co_cellvars = NULL;
self->_co_cached->_co_freevars = NULL;
self->_co_cached->_co_varnames = NULL;
else {
cached->_co_code = NULL;
cached->_co_cellvars = NULL;
cached->_co_freevars = NULL;
cached->_co_varnames = NULL;
FT_ATOMIC_STORE_PTR(self->_co_cached, cached);
}
}
return 0;

Py_END_CRITICAL_SECTION();
return cached != NULL ? 0 : -1;
}

/******************
* _PyCode_New()
******************/
Expand Down Expand Up @@ -1544,16 +1555,21 @@ get_cached_locals(PyCodeObject *co, PyObject **cached_field,
{
assert(cached_field != NULL);
assert(co->_co_cached != NULL);
if (*cached_field != NULL) {
return Py_NewRef(*cached_field);
PyObject *varnames = FT_ATOMIC_LOAD_PTR(*cached_field);
if (varnames != NULL) {
return Py_NewRef(varnames);
}
assert(*cached_field == NULL);
PyObject *varnames = get_localsplus_names(co, kind, num);

Py_BEGIN_CRITICAL_SECTION(co);
varnames = *cached_field;
if (varnames == NULL) {
return NULL;
varnames = get_localsplus_names(co, kind, num);
if (varnames != NULL) {
FT_ATOMIC_STORE_PTR(*cached_field, varnames);
}
}
*cached_field = Py_NewRef(varnames);
return varnames;
Py_END_CRITICAL_SECTION();
return Py_XNewRef(varnames);
}

PyObject *
Expand Down Expand Up @@ -1652,18 +1668,26 @@ _PyCode_GetCode(PyCodeObject *co)
if (init_co_cached(co)) {
return NULL;
}
if (co->_co_cached->_co_code != NULL) {
return Py_NewRef(co->_co_cached->_co_code);

_PyCoCached *cached = co->_co_cached;
PyObject *code = FT_ATOMIC_LOAD_PTR(cached->_co_code);
if (code != NULL) {
return Py_NewRef(code);
}
PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
_PyCode_NBYTES(co));

Py_BEGIN_CRITICAL_SECTION(co);
code = cached->_co_code;
if (code == NULL) {
return NULL;
code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
_PyCode_NBYTES(co));
if (code != NULL) {
deopt_code(co, (_Py_CODEUNIT *)PyBytes_AS_STRING(code));
assert(cached->_co_code == NULL);
FT_ATOMIC_STORE_PTR(cached->_co_code, code);
}
}
deopt_code(co, (_Py_CODEUNIT *)PyBytes_AS_STRING(code));
assert(co->_co_cached->_co_code == NULL);
co->_co_cached->_co_code = Py_NewRef(code);
return code;
Py_END_CRITICAL_SECTION();
return Py_XNewRef(code);
}

PyObject *
Expand Down
Loading