Skip to content

Commit 16aab70

Browse files
committed
Make _LOAD_ATTR_WITH_HINT thread-safe
1 parent 408e44b commit 16aab70

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

Python/bytecodes.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,16 +2217,30 @@ dummy_func(
22172217
PyObject *attr_o;
22182218

22192219
PyDictObject *dict = _PyObject_GetManagedDict(owner_o);
2220-
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries);
2220+
DEOPT_IF(!LOCK_OBJECT(dict));
2221+
if (hint >= (size_t)dict->ma_keys->dk_nentries) {
2222+
UNLOCK_OBJECT(dict);
2223+
DEOPT_IF(true);
2224+
}
22212225
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
2222-
DEOPT_IF(!DK_IS_UNICODE(dict->ma_keys));
2226+
if (!DK_IS_UNICODE(dict->ma_keys)) {
2227+
UNLOCK_OBJECT(dict);
2228+
DEOPT_IF(true);
2229+
}
22232230
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2224-
DEOPT_IF(ep->me_key != name);
2231+
if (ep->me_key != name) {
2232+
UNLOCK_OBJECT(dict);
2233+
DEOPT_IF(true);
2234+
}
22252235
attr_o = ep->me_value;
2226-
DEOPT_IF(attr_o == NULL);
2236+
if (attr_o == NULL) {
2237+
UNLOCK_OBJECT(dict);
2238+
DEOPT_IF(true);
2239+
}
22272240
STAT_INC(LOAD_ATTR, hit);
22282241
Py_INCREF(attr_o);
22292242
attr = PyStackRef_FromPyObjectSteal(attr_o);
2243+
UNLOCK_OBJECT(dict);
22302244
null = PyStackRef_NULL;
22312245
DECREF_INPUTS();
22322246
}

Python/executor_cases.c.h

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)