Skip to content

Commit 6f571fa

Browse files
committed
gh-158525: Avoid reacquiring dict lock when copying
1 parent e580c88 commit 6f571fa

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid reacquiring the source dictionary lock when copying a sparse dictionary
2+
in the free-threaded build.

‎Objects/dictobject.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4575,7 +4575,15 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict)
45754575
}
45764576
if (copy == NULL)
45774577
return NULL;
4578-
if (dict_merge(copy, o, 1, NULL) < 0) {
4578+
int res;
4579+
if (!PyFrozenDict_Check(o) && Py_TYPE(mp)->tp_iter == dict_iter) {
4580+
// The source is already locked and the copy is not yet visible.
4581+
res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL);
4582+
}
4583+
else {
4584+
res = dict_merge(copy, o, 1, NULL);
4585+
}
4586+
if (res < 0) {
45794587
Py_DECREF(copy);
45804588
return NULL;
45814589
}

0 commit comments

Comments
 (0)