Skip to content

Commit fee7d79

Browse files
DinoVfacebook-github-bot
authored andcommitted
Deliver dict added event only after it's guaranteed to succeed
Summary: Back port of: python/cpython#122207 fixing python/cpython#122208 The current dictionary watchers implementation delivers the added event before it checks to see if we need to re-size the dictionary. This resize can fail so the value isn't added, and then the tracker is out of sync with the true state of the dictionary. This moves the delivery of the event to after any necessary allocations have happened. Reviewed By: jbower-fb Differential Revision: D60182094 fbshipit-source-id: f34940e98ce1caadeee364f9d126d35839661961
1 parent bb3aca2 commit fee7d79

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Objects/dictobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,15 +1349,15 @@ insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
13491349
}
13501350

13511351
if (ix == DKIX_EMPTY) {
1352-
uint64_t new_version = _PyDict_NotifyEvent(
1353-
PyDict_EVENT_ADDED, mp, key, value);
13541352
/* Insert into new slot. */
13551353
assert(old_value == NULL);
13561354
if (mp->ma_keys->dk_usable <= 0) {
13571355
/* Need to resize. */
13581356
if (insertion_resize(mp) < 0)
13591357
goto Fail;
13601358
}
1359+
uint64_t new_version = _PyDict_NotifyEvent(
1360+
PyDict_EVENT_ADDED, mp, key, value);
13611361
if (!PyUnicode_CheckExact(key)) {
13621362
if (mp->ma_keys->dk_lookup == lookdict_with_lazy_imports_unicode) {
13631363
mp->ma_keys->dk_lookup = lookdict_with_lazy_imports;
@@ -1428,13 +1428,13 @@ insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
14281428
{
14291429
assert(mp->ma_keys == Py_EMPTY_KEYS);
14301430

1431-
uint64_t new_version = _PyDict_NotifyEvent(
1432-
PyDict_EVENT_ADDED, mp, key, value);
1433-
14341431
PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
14351432
if (newkeys == NULL) {
14361433
return -1;
14371434
}
1435+
uint64_t new_version = _PyDict_NotifyEvent(
1436+
PyDict_EVENT_ADDED, mp, key, value);
1437+
14381438
dictkeys_decref(Py_EMPTY_KEYS);
14391439
mp->ma_keys = newkeys;
14401440
mp->ma_values = NULL;
@@ -3517,14 +3517,14 @@ PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
35173517

35183518
if (ix == DKIX_EMPTY) {
35193519
PyDictKeyEntry *ep, *ep0;
3520-
uint64_t new_version = _PyDict_NotifyEvent(
3521-
PyDict_EVENT_ADDED, mp, key, defaultobj);
35223520
value = defaultobj;
35233521
if (mp->ma_keys->dk_usable <= 0) {
35243522
if (insertion_resize(mp) < 0) {
35253523
return NULL;
35263524
}
35273525
}
3526+
uint64_t new_version = _PyDict_NotifyEvent(
3527+
PyDict_EVENT_ADDED, mp, key, defaultobj);
35283528
if (!PyUnicode_CheckExact(key) && mp->ma_keys->dk_lookup != lookdict) {
35293529
mp->ma_keys->dk_lookup = lookdict;
35303530
}

0 commit comments

Comments
 (0)