Skip to content

Commit 011cb69

Browse files
committed
Revert "[3.15] gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (GH-150025) (#150039)"
This reverts commit 66ade28.
1 parent f523146 commit 011cb69

2 files changed

Lines changed: 7 additions & 30 deletions

File tree

Misc/NEWS.d/next/Library/2026-05-18-15-30-34.gh-issue-146452.RM0EVJ.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Modules/_pickle.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,9 +3450,6 @@ batch_dict(PickleState *state, PicklerObject *self, PyObject *iter, PyObject *or
34503450
* Returns 0 on success, -1 on error.
34513451
*
34523452
* Note that this currently doesn't work for protocol 0.
3453-
3454-
* gh-146452: Wrap the dict iteration in a critical sections to prevent
3455-
* concurrent mutation from invalidating PyDict_Next() iteration state.
34563453
*/
34573454
static int
34583455
batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
@@ -3469,24 +3466,15 @@ batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
34693466
assert(self->proto > 0);
34703467

34713468
dict_size = PyDict_GET_SIZE(obj);
3469+
assert(dict_size);
34723470

34733471
/* Write in batches of BATCHSIZE. */
34743472
Py_ssize_t total = 0;
34753473
do {
34763474
if (dict_size - total == 1) {
3477-
int next;
3478-
Py_BEGIN_CRITICAL_SECTION(obj);
3479-
next = PyDict_Next(obj, &ppos, &key, &value);
3480-
if (next) {
3481-
Py_INCREF(key);
3482-
Py_INCREF(value);
3483-
}
3484-
Py_END_CRITICAL_SECTION();
3485-
if (!next) {
3486-
PyErr_SetString(PyExc_RuntimeError,
3487-
"dictionary changed size during iteration");
3488-
goto error;
3489-
}
3475+
PyDict_Next(obj, &ppos, &key, &value);
3476+
Py_INCREF(key);
3477+
Py_INCREF(value);
34903478
if (save(state, self, key, 0) < 0) {
34913479
goto error;
34923480
}
@@ -3504,18 +3492,9 @@ batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
35043492
i = 0;
35053493
if (_Pickler_Write(self, &mark_op, 1) < 0)
35063494
return -1;
3507-
int next;
3508-
while (1) {
3509-
Py_BEGIN_CRITICAL_SECTION(obj);
3510-
next = PyDict_Next(obj, &ppos, &key, &value);
3511-
if (next) {
3512-
Py_INCREF(key);
3513-
Py_INCREF(value);
3514-
}
3515-
Py_END_CRITICAL_SECTION();
3516-
if (!next) {
3517-
break;
3518-
}
3495+
while (PyDict_Next(obj, &ppos, &key, &value)) {
3496+
Py_INCREF(key);
3497+
Py_INCREF(value);
35193498
if (save(state, self, key, 0) < 0) {
35203499
goto error;
35213500
}

0 commit comments

Comments
 (0)