Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-153881: Atomically load dk_nentries in `_PyObject_IsInstanceDict…
…Empty`
  • Loading branch information
brijkapadia committed Jul 18, 2026
commit 8976129dd2bf0d21493a4e22c067059916b066d2
20 changes: 20 additions & 0 deletions Lib/test/test_free_threading/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,25 @@ def reader():
with threading_helper.start_threads([t1, t2]):
pass

def test_getstate_race_with_shared_keys(self):
box = [None]
enter = Barrier(2)
leave = Barrier(2)

def reader():
for _ in range(1000):
enter.wait()
box[0].__getstate__()
leave.wait()

def writer():
for i in range(1000):
box[0] = type(f"C{i}", (), {})()
enter.wait()
setattr(box[0], str(i), 1)
leave.wait()

threading_helper.run_concurrently([reader, writer])

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix potential data race when calling :meth:`object.__getstate__`.
Comment thread
brijkapadia marked this conversation as resolved.
Outdated
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7722,7 +7722,7 @@ _PyObject_IsInstanceDictEmpty(PyObject *obj)
PyDictValues *values = _PyObject_InlineValues(obj);
if (FT_ATOMIC_LOAD_UINT8(values->valid)) {
PyDictKeysObject *keys = CACHED_KEYS(tp);
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
for (Py_ssize_t i = 0; i < LOAD_KEYS_NENTRIES(keys); i++) {
if (FT_ATOMIC_LOAD_PTR_RELAXED(values->values[i]) != NULL) {
return 0;
}
Expand Down
Loading