-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-120608: Make reversed thread-safe #120609
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 @@ | ||
Make :func:`reversed` thread-safe. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
#include "Python.h" | ||||||||||||||||||||||||||||||||||||||||||||||
#include "pycore_call.h" // _PyObject_CallNoArgs() | ||||||||||||||||||||||||||||||||||||||||||||||
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() | ||||||||||||||||||||||||||||||||||||||||||||||
#include "pycore_long.h" // _PyLong_GetOne() | ||||||||||||||||||||||||||||||||||||||||||||||
#include "pycore_modsupport.h" // _PyArg_NoKwnames() | ||||||||||||||||||||||||||||||||||||||||||||||
#include "pycore_object.h" // _PyObject_GC_TRACK() | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -431,16 +432,20 @@ reversed_next(reversedobject *ro) | |||||||||||||||||||||||||||||||||||||||||||||
PyObject *item; | ||||||||||||||||||||||||||||||||||||||||||||||
Py_ssize_t index = ro->index; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Py_BEGIN_CRITICAL_SECTION(ro); | ||||||||||||||||||||||||||||||||||||||||||||||
if (index >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
item = PySequence_GetItem(ro->seq, index); | ||||||||||||||||||||||||||||||||||||||||||||||
if (item != NULL) { | ||||||||||||||||||||||||||||||||||||||||||||||
ro->index--; | ||||||||||||||||||||||||||||||||||||||||||||||
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. We don't declare the critical section for the list_next Lines 3890 to 3911 in dacc5ac
Why do we have to declare the critical section here? cc @colesbury 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. The check |
||||||||||||||||||||||||||||||||||||||||||||||
Py_EXIT_CRITICAL_SECTION(); | ||||||||||||||||||||||||||||||||||||||||||||||
return item; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
if (PyErr_ExceptionMatches(PyExc_IndexError) || | ||||||||||||||||||||||||||||||||||||||||||||||
PyErr_ExceptionMatches(PyExc_StopIteration)) | ||||||||||||||||||||||||||||||||||||||||||||||
PyErr_Clear(); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
Py_END_CRITICAL_SECTION(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
ro->index = -1; | ||||||||||||||||||||||||||||||||||||||||||||||
Py_CLEAR(ro->seq); | ||||||||||||||||||||||||||||||||||||||||||||||
return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is better to define a local API instead of a global one? For example
END_TYPE_LOCK()
does that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just extract the critical section into a helper function and then wrap the helper with the existing macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this PR, I'd just extract most of
reversed_next
into areversed_next_impl
and wrap that with thePy_{BEGIN,END}_CRITICAL_SECTION
macros. See also #120318.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW we ended up not doing that in #120318. There's this comment though that explains the approach and gives an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback everyone! I added the
Py_EXIT_CRITICAL_SECTION
in this PR (and some others) to deal with return and goto statements. I am not in favor of adding a local API, since it will leak details of the locking implementation to modules. Changing thePy_EXIT_CRITICAL_SECTION
to a more private_Py_EXIT_CRITICAL_SECTION
(or something like that) would be fine with me.In this case we can indeed refactor to create a
reversed_next_impl
and put a global lock around it. I am not too worried about performance here, and if it turns out performance is an issue we can change later.Unless more suggestions come it, I will refactor to
reversed_next_impl
in the coming days.