Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-141805: Fix crash after concurrent addition objects with the same hash to set #143815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
serhiy-storchaka wants to merge 1 commit into python:main
base: main
Choose a base branch
Loading
from serhiy-storchaka:set-concurrent-add
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
28 changes: 28 additions & 0 deletions Lib/test/test_set.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ def test_iter_and_mutate(self):
list(si)

def test_merge_and_mutate(self):
# gh-141805
class X:
def __hash__(self):
return hash(0)
Expand All @@ -1865,6 +1866,33 @@ def __eq__(self, o):
s = {0}
s.update(other)

def test_hash_collision_concurrent_add(self):
class X:
def __hash__(self):
return 0
class Y:
flag = False
def __hash__(self):
return 0
def __eq__(self, other):
if not self.flag:
self.flag = True
s.add(X())
return self is other

a = X()
s = set()
s.add(a)
s.add(X())
s.remove(a)
# Now the set contains a dummy entry followed by an entry
# for an object with hash 0.
s.add(Y())
# The following operations should not crash.
repr(s)
list(s)
set() | s


class TestOperationsMutating:
"""Regression test for bpo-46615"""
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix crash in :class:`set` when objects with the same hash are concurrently
added to the set after removing an element with the same hash while the set
still contains elements with the same hash.
3 changes: 3 additions & 0 deletions Objects/setobject.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ set_add_entry_takeref(PySetObject *so, PyObject *key, Py_hash_t hash)
found_unused_or_dummy:
if (freeslot == NULL)
goto found_unused;
if (freeslot->hash != -1) {
goto restart;
}
FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, so->used + 1);
FT_ATOMIC_STORE_SSIZE_RELAXED(freeslot->hash, hash);
FT_ATOMIC_STORE_PTR_RELEASE(freeslot->key, key);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /