-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144513: Skip critical section locking during stop-the-world #144524
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix potential deadlock when using critical sections during stop-the-world | ||
| pauses in the free-threaded build. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| #include "Python.h" | ||
|
|
||
| #include "pycore_lock.h" | ||
| #include "pycore_critical_section.h" | ||
| #include "pycore_interp.h" | ||
| #include "pycore_lock.h" | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| static_assert(_Alignof(PyCriticalSection) >= 4, | ||
|
|
@@ -42,6 +43,15 @@ _PyCriticalSection_BeginSlow(PyThreadState *tstate, PyCriticalSection *c, PyMute | |
| } | ||
| } | ||
| } | ||
| // If the world is stopped, we don't need to acquire the lock because | ||
| // there are no other threads that could be accessing the object. | ||
| // Without this check, acquiring a critical section while the world is | ||
| // stopped could lead to a deadlock. | ||
| if (tstate->interp->stoptheworld.world_stopped) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this safe to read non-atomically (in theory as well as in practice), or is it just that we don't have a way of reading a bool atomically? I see comments about HEAD_LOCK protecting these values but in practice world_stopped is written to without the lock, and read all over the place, without apparent synchronization.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's safe (as long as you have a valid PyThreadState). It's only written to when once all other threads are stopped, so no extra synchronization is needed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should probably make this more clear in the |
||
| c->_cs_mutex = NULL; | ||
| c->_cs_prev = 0; | ||
| return; | ||
| } | ||
| c->_cs_mutex = NULL; | ||
| c->_cs_prev = (uintptr_t)tstate->critical_section; | ||
| tstate->critical_section = (uintptr_t)c; | ||
|
|
@@ -56,6 +66,12 @@ _PyCriticalSection2_BeginSlow(PyThreadState *tstate, PyCriticalSection2 *c, PyMu | |
| int is_m1_locked) | ||
| { | ||
| #ifdef Py_GIL_DISABLED | ||
| if (tstate->interp->stoptheworld.world_stopped) { | ||
| c->_cs_base._cs_mutex = NULL; | ||
| c->_cs_mutex2 = NULL; | ||
| c->_cs_base._cs_prev = 0; | ||
| return; | ||
| } | ||
| c->_cs_base._cs_mutex = NULL; | ||
| c->_cs_mutex2 = NULL; | ||
| c->_cs_base._cs_prev = tstate->critical_section; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.