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: avoid increase the set->used twice when the __eq__ trigger another add #142007

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
kemingy wants to merge 6 commits into python:main
base: main
Choose a base branch
Loading
from kemingy:gh-141805

Conversation

@kemingy
Copy link
Contributor

@kemingy kemingy commented Nov 27, 2025

gh-141805: goes directly to the found_unused_or_dummy when finding a dummy slot in set.add

This avoids triggering the __eq__ in the next iteration by the PyObject_RichCompareBool, which might introduce another set.add in the user code. Like:

tasks = set()
class Corrupt:
 def __eq__(self, value):
 tasks.add(self)
 return False

As described in #141805 (comment)

yihong0618 reacted with thumbs up emoji
...er add
Signed-off-by: Keming <kemingy94@gmail.com>
@kemingy kemingy changed the title (削除) fix: avoid increase the set->used twice when the __eq__ trigger another add (削除ここまで) (追記) gh-141805: avoid increase the set->used twice when the __eq__ trigger another add (追記ここまで) Nov 27, 2025
Signed-off-by: Keming <kemingy94@gmail.com>
Copy link
Member

@efimov-mikhail efimov-mikhail left a comment

Choose a reason for hiding this comment

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

This PR looks correct for me, just little nitpicks.

@efimov-mikhail efimov-mikhail added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 20, 2025
Copy link
Member

@efimov-mikhail efimov-mikhail left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor Author

kemingy commented Jan 8, 2026

Kindly ping @rhettinger, please take a look when you are free. Thanks!

else if (entry->hash == -1) {
assert (entry->key == dummy);
freeslot = entry;
goto found_unused_or_dummy;
Copy link
Member

@serhiy-storchaka serhiy-storchaka Jan 13, 2026

Choose a reason for hiding this comment

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

No, this is incorrect.

Imagine you added two elements with the same hash, A and B. Then A hes been removed, and replaced with a dummy (this is why dummy is needed). Then we try to add B again. With your code, it will find a dummy in place of A, and replace it with B. Now you have two Bs in a set.

Copy link
Member

@serhiy-storchaka serhiy-storchaka Jan 13, 2026

Choose a reason for hiding this comment

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

#143781 adds a test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

@efimov-mikhail efimov-mikhail efimov-mikhail approved these changes

@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger is a code owner

+1 more reviewer

@dr-carlos dr-carlos dr-carlos requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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