-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
gh-158600: Fix a race condition in concurrent set.intersection_update() #158673
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
base: main
Are you sure you want to change the base?
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 @@ | ||
| Fix a race condition in :meth:`set.intersection_update` on free-threaded builds that could cause concurrent updates to the same set to be lost. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1861,15 +1861,41 @@ set_intersection_update_multi_impl(PySetObject *so, PyObject * const *others, | |
| Py_ssize_t others_length) | ||
| /*[clinic end generated code: output=d768b5584675b48d input=782e422fc370e4fc]*/ | ||
| { | ||
| PyObject *tmp; | ||
| Py_ssize_t i; | ||
| PyObject *other; | ||
| PyObject *result; | ||
|
|
||
| tmp = set_intersection_multi_impl(so, others, others_length); | ||
| if (tmp == NULL) | ||
| if (others_length == 0) { | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| other = others[0]; | ||
| Py_BEGIN_CRITICAL_SECTION2(so, other); | ||
|
Contributor
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. Using
Contributor
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 think this shouldn't be a big deal for the main branch because we already check that inside
Contributor
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. Ah, you're right |
||
| result = set_intersection(so, other); | ||
| if (result != NULL) { | ||
| for (i = 1; i < others_length; i++) { | ||
| PyObject *newresult; | ||
|
|
||
| other = others[i]; | ||
| Py_BEGIN_CRITICAL_SECTION(other); | ||
|
Contributor
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. Nesting
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. If I understand correctly (please correct me if I am wrong), the ABBA deadlock will not happen because For example, suppose Thread 1 has completed its outer critical section on The reverse execution is also possible, depending on the execution order. But this leads to a problem of lost update (again)! Another thread can modify the target set while its lock is temporarily released. I will fix this. |
||
| newresult = set_intersection((PySetObject *)result, other); | ||
| Py_END_CRITICAL_SECTION(); | ||
| if (newresult == NULL) { | ||
| Py_CLEAR(result); | ||
| break; | ||
| } | ||
| Py_SETREF(result, newresult); | ||
| } | ||
| if (result != NULL) { | ||
| set_swap_bodies(so, (PySetObject *)result); | ||
| } | ||
| } | ||
| Py_END_CRITICAL_SECTION2(); | ||
|
|
||
| if (result == NULL) { | ||
| return NULL; | ||
| Py_BEGIN_CRITICAL_SECTION(so); | ||
| set_swap_bodies(so, (PySetObject *)tmp); | ||
| Py_END_CRITICAL_SECTION(); | ||
| Py_DECREF(tmp); | ||
| } | ||
| Py_DECREF(result); | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.