Skip to content

gh-117657: Fix data races reported by TSAN on interp->threads.main #118865

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 4 commits into from
May 10, 2024
Merged
Changes from 1 commit
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
Next Next commit
Fix data races reported by TSAN on interp->threads.main
Use relaxed loads/stores when reading/writing to this field.

This fixes races like https://gist.github.com/mpage/e07497ad8dd444a789ff306cb7996acc
  • Loading branch information
mpage committed May 9, 2024
commit 50cf12b2e9d73d20723ef7fbc0d5aad27edbbfb0
31 changes: 20 additions & 11 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,17 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
}
#endif

static inline void
set_main_thread(PyInterpreterState *interp, PyThreadState *tstate)
{
_Py_atomic_store_ptr_relaxed(&interp->threads.main, tstate);
}

static inline PyThreadState *
get_main_thread(PyInterpreterState *interp)
{
return _Py_atomic_load_ptr_relaxed(&interp->threads.main);
}

int
_PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
Expand All @@ -1052,21 +1063,22 @@ _PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
"current tstate has wrong interpreter");
return -1;
}
interp->threads.main = tstate;
set_main_thread(interp, tstate);

return 0;
}

void
_PyInterpreterState_SetNotRunningMain(PyInterpreterState *interp)
{
assert(interp->threads.main == current_fast_get());
interp->threads.main = NULL;
assert(get_main_thread(interp) == current_fast_get());
set_main_thread(interp, NULL);
}

int
_PyInterpreterState_IsRunningMain(PyInterpreterState *interp)
{
if (interp->threads.main != NULL) {
if (get_main_thread(interp) != NULL) {
return 1;
}
// Embedders might not know to call _PyInterpreterState_SetRunningMain(),
Expand All @@ -1082,18 +1094,15 @@ int
_PyThreadState_IsRunningMain(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
if (interp->threads.main != NULL) {
return tstate == interp->threads.main;
}
// See the note in _PyInterpreterState_IsRunningMain() about
// possible false negatives here for embedders.
return 0;
return get_main_thread(interp) == tstate;
}

int
_PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp)
{
if (interp->threads.main != NULL) {
if (get_main_thread(interp) != NULL) {
PyErr_SetString(PyExc_InterpreterError,
"interpreter already running");
return -1;
Expand All @@ -1105,8 +1114,8 @@ void
_PyInterpreterState_ReinitRunningMain(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
if (interp->threads.main != tstate) {
interp->threads.main = NULL;
if (get_main_thread(interp) != tstate) {
set_main_thread(interp, NULL);
}
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy