Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jan 13, 2026

This happens when the set contained several elements with the same hash, and then some of them were removed.

… same hash to set

This happens when the set contained several elements with the same hash,
and then some of them were removed.
@skirpichev
Copy link
Member

I think this also fixes #143546. (N.B.: on my system I can't trigger a crash with left &= right from issue, but left |= right - "works" for me.)

found_unused_or_dummy:
if (freeslot == NULL)
goto found_unused;
if (freeslot->hash != -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment explaining this case? I understand that it occurs if the set is modified indirectly by a PyObject_RichCompareBool() call.

Copy link
Contributor

@rhettinger rhettinger left a comment

Choose a reason for hiding this comment

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

It took me a while to trace through this, but the fix looks correct.

All it does it verify that what was a dummy entry during the search is still a dummy entry when we're about to fill it. The root of the bug is that the PyObject_RichCompareBool call can reenter and fill the dummy slot while the original addition is still in-flight.

The cost of the edit is near zero. It only affects filling dummy objects, the entry->hash will already be in L1 cache, and the branch is 100% predictable.

@serhiy-storchaka serhiy-storchaka merged commit b8e925b into python:main Jan 14, 2026
57 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 14, 2026
… same hash to set (pythonGH-143815)

This happens when the set contained several elements with the same hash,
and then some of them were removed.
(cherry picked from commit b8e925b4f8f6c5e28fbebc4f3965bf77610698b3)

Co-authored-by: Serhiy Storchaka <[email protected]>
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b8e925b4f8f6c5e28fbebc4f3965bf77610698b3 3.13

@serhiy-storchaka serhiy-storchaka deleted the set-concurrent-add branch January 14, 2026 21:29
@bedevere-app
Copy link

bedevere-app bot commented Jan 14, 2026

GH-143849 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jan 14, 2026
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jan 15, 2026
…ith the same hash to set (pythonGH-143815)

This happens when the set contained several elements with the same hash,
and then some of them were removed.
(cherry picked from commit b8e925b)

Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 15, 2026

GH-143853 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 15, 2026
serhiy-storchaka added a commit that referenced this pull request Jan 15, 2026
…e same hash to set (GH-143815) (GH-143849)

This happens when the set contained several elements with the same hash,
and then some of them were removed.
(cherry picked from commit b8e925b)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this pull request Jan 15, 2026
…e same hash to set (GH-143815) (GH-143853)

This happens when the set contained several elements with the same hash,
and then some of them were removed.
(cherry picked from commit b8e925b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants