Skip to content

Commit 6d5e97f

Browse files
mpagemiss-islington
authored andcommitted
pythongh-117657: Fix data races reported by TSAN on interp->threads.main (pythonGH-118865)
Use relaxed loads/stores when reading/writing to this field. (cherry picked from commit 22d5185) Co-authored-by: mpage <mpage@meta.com>
1 parent 93fce2d commit 6d5e97f

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix data races on the field that stores a pointer to the interpreter's main thread that occur in free-threaded builds.

Python/pystate.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,17 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
10381038
}
10391039
#endif
10401040

1041+
static inline void
1042+
set_main_thread(PyInterpreterState *interp, PyThreadState *tstate)
1043+
{
1044+
_Py_atomic_store_ptr_relaxed(&interp->threads.main, tstate);
1045+
}
1046+
1047+
static inline PyThreadState *
1048+
get_main_thread(PyInterpreterState *interp)
1049+
{
1050+
return _Py_atomic_load_ptr_relaxed(&interp->threads.main);
1051+
}
10411052

10421053
int
10431054
_PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
@@ -1052,21 +1063,22 @@ _PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
10521063
"current tstate has wrong interpreter");
10531064
return -1;
10541065
}
1055-
interp->threads.main = tstate;
1066+
set_main_thread(interp, tstate);
1067+
10561068
return 0;
10571069
}
10581070

10591071
void
10601072
_PyInterpreterState_SetNotRunningMain(PyInterpreterState *interp)
10611073
{
1062-
assert(interp->threads.main == current_fast_get());
1063-
interp->threads.main = NULL;
1074+
assert(get_main_thread(interp) == current_fast_get());
1075+
set_main_thread(interp, NULL);
10641076
}
10651077

10661078
int
10671079
_PyInterpreterState_IsRunningMain(PyInterpreterState *interp)
10681080
{
1069-
if (interp->threads.main != NULL) {
1081+
if (get_main_thread(interp) != NULL) {
10701082
return 1;
10711083
}
10721084
// Embedders might not know to call _PyInterpreterState_SetRunningMain(),
@@ -1082,18 +1094,15 @@ int
10821094
_PyThreadState_IsRunningMain(PyThreadState *tstate)
10831095
{
10841096
PyInterpreterState *interp = tstate->interp;
1085-
if (interp->threads.main != NULL) {
1086-
return tstate == interp->threads.main;
1087-
}
10881097
// See the note in _PyInterpreterState_IsRunningMain() about
10891098
// possible false negatives here for embedders.
1090-
return 0;
1099+
return get_main_thread(interp) == tstate;
10911100
}
10921101

10931102
int
10941103
_PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp)
10951104
{
1096-
if (interp->threads.main != NULL) {
1105+
if (get_main_thread(interp) != NULL) {
10971106
PyErr_SetString(PyExc_InterpreterError,
10981107
"interpreter already running");
10991108
return -1;
@@ -1105,8 +1114,8 @@ void
11051114
_PyInterpreterState_ReinitRunningMain(PyThreadState *tstate)
11061115
{
11071116
PyInterpreterState *interp = tstate->interp;
1108-
if (interp->threads.main != tstate) {
1109-
interp->threads.main = NULL;
1117+
if (get_main_thread(interp) != tstate) {
1118+
set_main_thread(interp, NULL);
11101119
}
11111120
}
11121121

Tools/tsan/suppressions_free_threading.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ race_top:_mi_heap_delayed_free_partial
3333
race_top:_PyEval_EvalFrameDefault
3434
race_top:_PyImport_AcquireLock
3535
race_top:_PyImport_ReleaseLock
36-
race_top:_PyInterpreterState_SetNotRunningMain
37-
race_top:_PyInterpreterState_IsRunningMain
3836
# https://gist.github.com/mpage/0a24eb2dd458441ededb498e9b0e5de8
3937
race_top:_PyParkingLot_Park
4038
race_top:_PyType_HasFeature

0 commit comments

Comments
 (0)
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