10000 [3.13] gh-131113: Fix data race in dict.popitem() (gh-131115) (#131119) · python/cpython@9e0fce4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e0fce4

Browse files
[3.13] gh-131113: Fix data race in dict.popitem() (gh-131115) (#131119)
The clearing of the key, hash, and value need to use atomic operations to avoid a data race with concurrent read operations. (cherry picked from commit c00ac57) Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent 98c7ae3 commit 9e0fce4

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Objects/dictobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ load_keys_nentries(PyDictObject *mp)
275275

276276
#endif
277277

278-
#define STORE_KEY(ep, key) FT_ATOMIC_STORE_PTR_RELEASE(ep->me_key, key)
279-
#define STORE_VALUE(ep, value) FT_ATOMIC_STORE_PTR_RELEASE(ep->me_value, value)
278+
#define STORE_KEY(ep, key) FT_ATOMIC_STORE_PTR_RELEASE((ep)->me_key, key)
279+
#define STORE_VALUE(ep, value) FT_ATOMIC_STORE_PTR_RELEASE((ep)->me_value, value)
280280
#define STORE_SPLIT_VALUE(mp, idx, value) FT_ATOMIC_STORE_PTR_RELEASE(mp->ma_values->values[idx], value)
281-
#define STORE_HASH(ep, hash) FT_ATOMIC_STORE_SSIZE_RELAXED(ep->me_hash, hash)
281+
#define STORE_HASH(ep, hash) FT_ATOMIC_STORE_SSIZE_RELAXED((ep)->me_hash, hash)
282282
#define STORE_KEYS_USABLE(keys, usable) FT_ATOMIC_STORE_SSIZE_RELAXED(keys->dk_usable, usable)
283283
#define STORE_KEYS_NENTRIES(keys, nentries) FT_ATOMIC_STORE_SSIZE_RELAXED(keys->dk_nentries, nentries)
284284
#define STORE_USED(mp, used) FT_ATOMIC_STORE_SSIZE_RELAXED(mp->ma_used, used)
@@ -4465,8 +4465,8 @@ dict_popitem_impl(PyDictObject *self)
44654465
interp, PyDict_EVENT_DELETED, self, key, NULL);
44664466
hash = unicode_get_hash(key);
44674467
value = ep0[i].me_value;
4468-
ep0[i].me_key = NULL;
4469-
ep0[i].me_value = NULL;
4468+
STORE_KEY(&ep0[i], NULL);
4469+
STORE_VALUE(&ep0[i], NULL);
44704470
}
44714471
else {
44724472
PyDictKeyEntry *ep0 = DK_ENTRIES(self->ma_keys);
@@ -4481,9 +4481,9 @@ dict_popitem_impl(PyDictObject *self)
44814481
interp, PyDict_EVENT_DELETED, self, key, NULL);
44824482
hash = ep0[i].me_hash;
44834483
value = ep0[i].me_value;
4484-
ep0[i].me_key = NULL;
4485-
ep0[i].me_hash = -1;
4486-
ep0[i].me_value = NULL;
4484+
STORE_KEY(&ep0[i], NULL);
4485+
STORE_HASH(&ep0[i], -1);
4486+
STORE_VALUE(&ep0[i], NULL);
44874487
}
44884488

44894489
j = lookdict_index(self->ma_keys, hash, i);

0 commit comments

Comments
 (0)
0