Skip to content

gh-122201: Lock mutex when setting handling_thread to NULL #122204

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 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 15 additions & 3 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,18 @@ unsignal_pending_calls(PyThreadState *tstate, PyInterpreterState *interp)
#endif
}

static void
clear_pending_handling_thread(struct _pending_calls *pending)
{
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&pending->mutex);
pending->handling_thread = NULL;
PyMutex_Unlock(&pending->mutex);
#else
pending->handling_thread = NULL;
#endif
}

static int
make_pending_calls(PyThreadState *tstate)
{
Expand Down Expand Up @@ -933,7 +945,7 @@ make_pending_calls(PyThreadState *tstate)

int32_t npending;
if (_make_pending_calls(pending, &npending) != 0) {
pending->handling_thread = NULL;
clear_pending_handling_thread(pending);
/* There might not be more calls to make, but we play it safe. */
signal_pending_calls(tstate, interp);
return -1;
Expand All @@ -945,7 +957,7 @@ make_pending_calls(PyThreadState *tstate)

if (_Py_IsMainThread() && _Py_IsMainInterpreter(interp)) {
if (_make_pending_calls(pending_main, &npending) != 0) {
pending->handling_thread = NULL;
clear_pending_handling_thread(pending);
/* There might not be more calls to make, but we play it safe. */
signal_pending_calls(tstate, interp);
return -1;
Expand All @@ -956,7 +968,7 @@ make_pending_calls(PyThreadState *tstate)
}
}

pending->handling_thread = NULL;
clear_pending_handling_thread(pending);
return 0;
}

Expand Down
1 change: 0 additions & 1 deletion Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ race_top:assign_version_tag
race_top:new_reference
race_top:_multiprocessing_SemLock_acquire_impl
race_top:list_get_item_ref
race_top:make_pending_calls
race_top:_Py_slot_tp_getattr_hook
race_top:add_threadstate
race_top:dump_traceback
Expand Down
Loading