Skip to content

Commit

Permalink
pythongh-129824: Fix data races in type_ready with subinterpreters
Browse files Browse the repository at this point in the history
Also add test_interpreters to the list of TSAN tests. The test_running
and test_is_running test cases are skipped for now as they have file
descriptor races.
  • Loading branch information
colesbury committed Mar 3, 2025
1 parent 3a7f17c commit 383c4cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ def test_not_shareable(self):
with self.assertRaises(ExecutionFailed):
interp.exec('print(spam)')

@support.skip_if_sanitizer('gh-129824: file descriptor race', thread=True)
def test_running(self):
interp = interpreters.create()
interp.prepare_main({'spam': True})
Expand Down Expand Up @@ -1530,6 +1531,7 @@ def test_whence(self):
whence = eval(text)
self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI)

@support.skip_if_sanitizer('gh-129824: file descriptor race', thread=True)
def test_is_running(self):
def check(interpid, expected):
with self.assertRaisesRegex(InterpreterError, 'unrecognized'):
Expand Down
3 changes: 2 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -4350,7 +4350,8 @@ _PyExc_InitTypes(PyInterpreterState *interp)
return -1;
}
if (exc->tp_new == BaseException_new
&& exc->tp_init == BaseException_init)
&& exc->tp_init == BaseException_init
&& _Py_IsMainInterpreter(interp))
{
exc->tp_vectorcall = BaseException_vectorcall;
}
Expand Down
20 changes: 15 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
? index
: index + _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES;

assert((initial == 1) ==
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
(void)_Py_atomic_add_int64(
int64_t prev_interp_count = _Py_atomic_add_int64(
&_PyRuntime.types.managed_static.types[full_index].interp_count, 1);
assert((initial == 1) == (prev_interp_count == 0));
(void)prev_interp_count;

if (initial) {
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL);
Expand Down Expand Up @@ -8581,12 +8581,22 @@ type_ready_set_new(PyTypeObject *type, int initial)
}
else {
// tp_new is NULL: inherit tp_new from base
type->tp_new = base->tp_new;
if (initial) {
type->tp_new = base->tp_new;
}
else {
assert(type->tp_new == base->tp_new);
}
}
}
else {
// Py_TPFLAGS_DISALLOW_INSTANTIATION sets tp_new to NULL
type->tp_new = NULL;
if (initial) {
type->tp_new = NULL;
}
else {
assert(type->tp_new == NULL);
}
}
return 0;
}
Expand Down

0 comments on commit 383c4cc

Please sign in to comment.