GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-117657: Fix data races reported by TSAN on `interp->threads.main` … · python/cpython@22d5185

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Misc/NEWS.d/next/Core and Builtins
    • 2024-05-09-19-47-12.gh-issue-117657.Vn0Yey.rst
  • Python
    • pystate.c
  • Tools/tsan
    • suppressions_free_threading.txt
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Misc/NEWS.d/next/Core and Builtins/2024-05-09-19-47-12.gh-issue-117657.Vn0Yey.rst‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
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.
Collapse file

‎Python/pystate.c‎

Copy file name to clipboardExpand all lines: Python/pystate.c
+20-11Lines 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

Collapse file

‎Tools/tsan/suppressions_free_threading.txt‎

Copy file name to clipboardExpand all lines: Tools/tsan/suppressions_free_threading.txt
-2Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.