FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129824: Fix some data races with types in subinterpreters by colesbury · Pull Request #129829 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions Lib/test/test_interpreters/test_api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4231,7 +4231,8 @@ _PyExc_InitTypes(PyInterpreterState *interp)
return -1;
}
if (exc->tp_new == BaseException_new
&& exc->tp_init == (initproc)BaseException_init)
&& exc->tp_init == (initproc)BaseException_init
&& _Py_IsMainInterpreter(interp))
{
exc->tp_vectorcall = BaseException_vectorcall;
}
Expand Down
34 changes: 27 additions & 7 deletions Objects/typeobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,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 @@ -8506,7 +8506,12 @@ type_ready_set_new(PyTypeObject *type, int initial)
&& base == &PyBaseObject_Type
&& !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
{
type->tp_flags |= Py_TPFLAGS_DISALLOW_INSTANTIATION;
if (initial) {
type->tp_flags |= Py_TPFLAGS_DISALLOW_INSTANTIATION;
}
else {
assert(_PyType_HasFeature(type, Py_TPFLAGS_DISALLOW_INSTANTIATION));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm worried that these assertions might be racy. If two interpreters are trying to initialize the type at the same time, TSan might start complaining, because the store is non-atomic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

There's no race because tp_flags is only set in the initial interpreter (when initial=1), and all static types are initialized at interpreter startup.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I might not understand the type lifecycle correctly. I'm imagining a case where:

  • Two subinterpreters attempt to import a module that has not been initialized by any interpreter, and that module contains static types.
  • Interpreter A will get initial atomically, and then interpreter B falls back to the assertion.
  • If interpreter A sets tp_flags at the same time as interpreter B calls PyType_HasFeature, wouldn't that race?

Is there some extra layer of synchronization?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think that modules are always imported in the main interpreter first, but @ericsnowcurrently would know better:

cpython/Python/import.c

Lines 1973 to 1979 in 8d9d3e4

* To avoid all of that, we make sure the module's init function
* is always run first with the main interpreter active. If it was
* already the main interpreter then we can continue loading the
* module like normal. Otherwise, right after the init function,
* we take care of some import state bookkeeping, switch back
* to the subinterpreter, check for single-phase init,
* and then continue loading like normal. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hmm. I think that just means it will have a thread state under the main interpreter, which should synchronize it on a GILicious build, but there would be no synchronization under FT. It's probably worth investigating that once we get the rest of the type races fixed. I'll yield to you on whether to keep the assertions in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, this is more complicated than I originally thought. I'm going to close this for now.

}
}

if (!(type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION)) {
Expand All @@ -8521,12 +8526,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 Expand Up @@ -8659,7 +8674,12 @@ type_ready(PyTypeObject *type, int initial)
}

/* All done -- set the ready flag */
type->tp_flags |= Py_TPFLAGS_READY;
if (initial) {
type->tp_flags |= Py_TPFLAGS_READY;
}
else {
assert(_PyType_HasFeature(type, Py_TPFLAGS_READY));
}
stop_readying(type);

assert(_PyType_CheckConsistency(type));
Expand Down

Back | FazBrowse Home | New Git URL