Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
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__`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix potential data race when calling :meth:`object.__getstate__`.
Fix potential data race when calling :meth:`object.__getstate__` under the free-threaded build.

I think we should make it clear to users that this data race is only possible under the FT build.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use term:`free-threaded build` as well.

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