8000 gh-131113: Fix data race in dict.popitem() (gh-131115) · python/cpython@c00ac57 · GitHub
[go: up one dir, main page]

Skip to content

Commit c00ac57

Browse files
authored
gh-131113: Fix data race in dict.popitem() (gh-131115)
The clearing of the key, hash, and value need to use atomic operations to avoid a data race with concurrent read operations.
1 parent ad90c5f commit c00ac57

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)
@@ -4534,8 +4534,8 @@ dict_popitem_impl(PyDictObject *self)
45344534
_PyDict_NotifyEvent(interp, PyDict_EVENT_DELETED, self, key, NULL);
45354535
hash = unicode_get_hash(key);
45364536
value = ep0[i].me_value;
4537-
ep0[i].me_key = NULL;
4538-
ep0[i].me_value = NULL;
4537+
STORE_KEY(&ep0[i], NULL);
4538+
STORE_VALUE(&ep0[i], NULL);
45394539
}
45404540
else {
45414541
PyDictKeyEntry *ep0 = DK_ENTRIES(self->ma_keys);
@@ -4549,9 +4549,9 @@ dict_popitem_impl(PyDictObject *self)
45494549
_PyDict_NotifyEvent(interp, PyDict_EVENT_DELETED, self, key, NULL);
45504550
hash = ep0[i].me_hash;
45514551
value = ep0[i].me_value;
4552-
ep0[i].me_key = NULL;
4553-
ep0[i].me_hash = -1;
4554-
ep0[i].me_value = NULL;
4552+
STORE_KEY(&ep0[i], NULL);
4553+
STORE_HASH(&ep0[i], -1);
4554+
STORE_VALUE(&ep0[i], NULL);
45554555
}
45564556

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

0 commit comments

Comments
 (0)
0