-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Changes from all commits
b652577
2f04291
d12d8b6
1efd4e5
ddb87fe
bfdc05a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||||||
import unittest | ||||||||
|
||||||||
from threading import Thread, Barrier | ||||||||
from unittest import TestCase | ||||||||
|
||||||||
from test.support import threading_helper | ||||||||
|
||||||||
|
||||||||
@threading_helper.requires_working_threading() | ||||||||
class TestSet(TestCase): | ||||||||
def test_repr_clear(self): | ||||||||
"""Test repr() of a set while another thread is calling clear()""" | ||||||||
NUM_ITERS = 10 | ||||||||
NUM_REPR_THREADS = 10 | ||||||||
barrier = Barrier(NUM_REPR_THREADS + 1) | ||||||||
s = {1, 2, 3, 4, 5, 6, 7, 8} | ||||||||
|
||||||||
def clear_set(): | ||||||||
barrier.wait() | ||||||||
s.clear() | ||||||||
|
||||||||
def repr_set(): | ||||||||
barrier.wait() | ||||||||
set_reprs.append(repr(s)) | ||||||||
|
||||||||
for _ in range(NUM_ITERS): | ||||||||
set_reprs = [] | ||||||||
colesbury marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
threads = [Thread(target=clear_set)] | ||||||||
for _ in range(NUM_REPR_THREADS): | ||||||||
threads.append(Thread(target=repr_set)) | ||||||||
for t in threads: | ||||||||
t.start() | ||||||||
for t in threads: | ||||||||
t.join() | ||||||||
|
||||||||
for set_repr in set_reprs: | ||||||||
self.assertIn(set_repr, ("set()", "{1, 2, 3, 4, 5, 6, 7, 8}")) | ||||||||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion ended up not working: expected is set_reprs may be any of the three cases:
So we could do |
||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix a race condition in the :term:`free threading` build when ``repr(set)`` | ||
is called concurrently with ``set.clear()``. |
Uh oh!
There was an error while loading. Please reload this page.