FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-146452: Improve locking granularity in pickle's batch_dict_exact by scopreon · Pull Request #150025 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix race condition when pickling dictionaries in free threaded builds. Also
reduce critical section cover.
49 changes: 29 additions & 20 deletions Modules/_pickle.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3450,9 +3450,12 @@ batch_dict(PickleState *state, PicklerObject *self, PyObject *iter, PyObject *or
* Returns 0 on success, -1 on error.
*
* Note that this currently doesn't work for protocol 0.

* gh-146452: Wrap the dict iteration in a critical sections to prevent
* concurrent mutation from invalidating PyDict_Next() iteration state.
*/
static int
batch_dict_exact_impl(PickleState *state, PicklerObject *self, PyObject *obj)
batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
{
PyObject *key = NULL, *value = NULL;
int i;
Expand All @@ -3466,15 +3469,24 @@ batch_dict_exact_impl(PickleState *state, PicklerObject *self, PyObject *obj)
assert(self->proto > 0);

dict_size = PyDict_GET_SIZE(obj);
assert(dict_size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.


/* Write in batches of BATCHSIZE. */
Py_ssize_t total = 0;
do {
if (dict_size - total == 1) {
PyDict_Next(obj, &ppos, &key, &value);
Py_INCREF(key);
Py_INCREF(value);
int next;
Py_BEGIN_CRITICAL_SECTION(obj);
next = PyDict_Next(obj, &ppos, &key, &value);
if (next) {
Py_INCREF(key);
Py_INCREF(value);
}
Py_END_CRITICAL_SECTION();
if (!next) {
PyErr_SetString(PyExc_RuntimeError,
"dictionary changed size during iteration");
goto error;
}
if (save(state, self, key, 0) < 0) {
goto error;
}
Expand All @@ -3492,9 +3504,18 @@ batch_dict_exact_impl(PickleState *state, PicklerObject *self, PyObject *obj)
i = 0;
if (_Pickler_Write(self, &mark_op, 1) < 0)
return -1;
while (PyDict_Next(obj, &ppos, &key, &value)) {
Py_INCREF(key);
Py_INCREF(value);
int next;
while (1) {
Py_BEGIN_CRITICAL_SECTION(obj);
next = PyDict_Next(obj, &ppos, &key, &value);
if (next) {
Py_INCREF(key);
Py_INCREF(value);
}
Py_END_CRITICAL_SECTION();
if (!next) {
break;
}
if (save(state, self, key, 0) < 0) {
goto error;
}
Expand Down Expand Up @@ -3525,18 +3546,6 @@ batch_dict_exact_impl(PickleState *state, PicklerObject *self, PyObject *obj)
return -1;
}

/* 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;
}

static int
save_dict(PickleState *state, PicklerObject *self, PyObject *obj)
{
Expand Down
Loading

Back | FazBrowse Home | New Git URL