| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead. |
Sorry, something went wrong.
Sorry, something went wrong.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead. |
Sorry, something went wrong.
…ing) Replace the coarse critical section wrapping the entire function with fine-grained sections covering only PyDict_Next + Py_INCREF. Also handle PyDict_Next returning 0 in the single-item fast path.
| assert(self->proto > 0); | ||
|
|
||
| dict_size = PyDict_GET_SIZE(obj); | ||
| assert(dict_size); |
There was a problem hiding this comment.
We discussed this in person. This assertion could fail if the dictionary gets mutated by another thread between the size check in the caller and this check. The code should work correctly in this case (it will just emit a SETITEMS that sets 0 items), so removing the assertion is the right change.
Sorry, something went wrong.
|
Thanks @scopreon for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
Sorry, something went wrong.
|
Sorry, @scopreon and @JelleZijlstra, I could not cleanly backport this to 3.14 due to a conflict. cherry_picker 57a0e570d36f41b953a91bbaf4262a5d05d0391b 3.14 |
Sorry, something went wrong.
|
GH-150039 is a backport of this pull request to the 3.15 branch. |
Sorry, something went wrong.
…exact and fix race condition (GH-150025) (#150039) gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (GH-150025) Remove assertion that could fail in rare race condition. Replace the coarse critical section wrapping the entire function with fine-grained sections covering only PyDict_Next + Py_INCREF. Also handle PyDict_Next returning 0 in the single-item fast path. (cherry picked from commit 57a0e57) Co-authored-by: Saul Cooperman <58375603+scopreon@users.noreply.github.com>
|
GH-150062 is a backport of this pull request to the 3.14 branch. |
Sorry, something went wrong.
#150062) [3.14] gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (GH-150025) Remove assertion that could fail in rare race condition. Replace the coarse critical section wrapping the entire function with fine-grained sections covering only PyDict_Next + Py_INCREF. Also handle PyDict_Next returning 0 in the single-item fast path. (cherry picked from commit 57a0e57)
|
I propose to revert these changes, since they cause Out of Memory on the main branch when running test_free_threading.test_pickle. Because of that, the GitHub Action CI jobs fail with exit code 143 (issue gh-150114). I don't think that this change is correct: sometimes (race condition), test_pickle_dumps_with_concurrent_dict_mutation() of test_free_threading.test_pickle enters an unlimited loop and exhaust the system memory. See the issue gh-150257. The following code was added to _pickle to fix the crash gh-146452: /* gh-146452: Wrap the dict iteration in a critical section to prevent
concurrent mutation from invalidating PyDict_Next() iteration state. */
static int
batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
{
int ret;
Py_BEGIN_CRITICAL_SECTION(obj);
ret = batch_dict_exact_impl(state, self, obj);
Py_END_CRITICAL_SECTION();
return ret;
}
This PR replaces this function with "fine-grained sections". Problem: Py_BEGIN_CRITICAL_SECTION(obj); and Py_END_CRITICAL_SECTION(); is put around a single PyDict_Next() call, whereas the PyDict_Next() documentation recommends putting the whole loop in the critical section. By the way, the 3.15 backport of this PR is different since there was not batch_dict_exact_impl() function. Maybe a 3.15 backport of the previous fix was missed? |
Sorry, something went wrong.
|
I prepared 3 reverts:
Once the 3.15 revert will be merged, the commit e62a611 should be backported to the 3.15 branch. |
Sorry, something went wrong.
|
Thanks for spotting this regression @vstinner , and for the explanation. Sorry for overlooking that. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Improve critical section locking in batch_dict_exact. This originally held a lock for the entirey of batch_dict_exact_impl, which is not necessary. We can instead have localised locks for sections which require it. It wrapped possibly expensive calls to save() etc
Remove batch_dict_exact_impl which existed to allow the wrapping with critical sections. Called by batch_dict_exact.
Additionally handle the case where PyDict_Next returns 0 in the size 1 special case (modified during pickling).
Note: this spun out of looking into #149816 89, it was fixed in #146452.
Test was added in https://github.com/python/cpython/pull/146470/changes