Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Lib/test/test_free_threading/test_pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pickle
import threading
import unittest

from test.support import threading_helper


@threading_helper.requires_working_threading()
class TestPickleFreeThreading(unittest.TestCase):

def test_pickle_dumps_with_concurrent_dict_mutation(self):
# gh-146452: Pickling a dict while another thread mutates it
# used to segfault. batch_dict_exact() iterated dict items via
# PyDict_Next() which returns borrowed references, and a
# concurrent pop/replace could free the value before Py_INCREF
# got to it.
shared = {str(i): list(range(20)) for i in range(50)}

def dumper():
for _ in range(1000):
try:
pickle.dumps(shared)
except RuntimeError:
# "dictionary changed size during iteration" is expected
pass

def mutator():
for j in range(1000):
key = str(j % 50)
shared[key] = list(range(j % 20))
if j % 10 == 0:
shared.pop(key, None)
shared[key] = [j]

threads = []
for _ in range(10):
threads.append(threading.Thread(target=dumper))
threads.append(threading.Thread(target=mutator))

for t in threads:
t.start()
for t in threads:
t.join()

# If we get here without a segfault, the test passed.


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix segfault in :mod:`pickle` when pickling a dictionary concurrently
mutated by another thread in the free-threaded build. The dict iteration in
``batch_dict_exact`` now holds a critical section to prevent borrowed
references from being invalidated mid-iteration.
14 changes: 13 additions & 1 deletion Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3451,7 +3451,7 @@ batch_dict(PickleState *state, PicklerObject *self, PyObject *iter, PyObject *or
* Note that this currently doesn't work for protocol 0.
*/
static int
batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
batch_dict_exact_impl(PickleState *state, PicklerObject *self, PyObject *obj)
{
PyObject *key = NULL, *value = NULL;
int i;
Expand Down Expand Up @@ -3524,6 +3524,18 @@ batch_dict_exact(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