Skip to content

gh-129967: Fix race condition in repr(set) #129978

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

Merged
merged 6 commits into from
Feb 11, 2025

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Feb 10, 2025

In the free threaded build, the call to PySequence_List() could temporarily unlock and relock the set, allowing the items to be cleared and return the incorrect notation {} for a empty set (it should be set()).

The call to `PySequence_List()` could temporarily unlock and relock the
set, allowing the items to be cleared and return the incorrect
notation `{}` for a empty set (it should be `set()`).
Comment on lines +38 to +39
for set_repr in set_reprs:
self.assertIn(set_repr, ("set()", "{1, 2, 3, 4, 5, 6, 7, 8}"))
Copy link
Member

Choose a reason for hiding this comment

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

... this would report all failing set reprs, not just the first one, and avoid hardcoding the repr we're expecting. (4/4)

Suggested change
for set_repr in set_reprs:
self.assertIn(set_repr, ("set()", "{1, 2, 3, 4, 5, 6, 7, 8}"))
self.assertEqual(set_reprs, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This assertion ended up not working:

expected is { "set()", "{1, 2, 3, 4, 5, 6, 7, 8}" }

set_reprs may be any of the three cases:

  • { "set()", "{1, 2, 3, 4, 5, 6, 7, 8}" }
  • { "{1, 2, 3, 4, 5, 6, 7, 8}" }
  • { "set()" }

So we could do self.assertTrue(set_reprs.issubset(expected)) or something similar, but at that point I'd prefer the original assertion failure message.

@colesbury colesbury merged commit a7427f2 into python:main Feb 11, 2025
42 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@colesbury colesbury deleted the gh-129967-set-repr-clear branch February 11, 2025 22:29
@miss-islington-app
Copy link

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

cherry_picker a7427f2db937adb4c787754deb4c337f1894fe86 3.13

@bedevere-app
Copy link

bedevere-app bot commented Feb 11, 2025

GH-130020 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 Feb 11, 2025
colesbury added a commit to colesbury/cpython that referenced this pull request Feb 11, 2025
…29978)

The call to `PySequence_List()` could temporarily unlock and relock the
set, allowing the items to be cleared and return the incorrect
notation `{}` for a empty set (it should be `set()`).
(cherry picked from commit a7427f2)

Co-authored-by: Sam Gross <colesbury@gmail.com>
Co-authored-by: T. Wouters <thomas@python.org>
colesbury added a commit that referenced this pull request Feb 11, 2025
…30020)

The call to `PySequence_List()` could temporarily unlock and relock the
set, allowing the items to be cleared and return the incorrect
notation `{}` for a empty set (it should be `set()`).

(cherry picked from commit a7427f2)

Co-authored-by: T. Wouters <thomas@python.org>
@colesbury colesbury removed their assignment Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants