Repository navigation
Conversation
Contributor
Author
|
I have attempted writing a test for this, but it's very unreliable and it requires adding a sleep in There's also the problem that the passing test doesn't prove that the race cannot happen, so a unit test is probably not the right way to go anyway. Let me know if you have suggestions as to how to test this. The test (written by the clanker): def test_getitem_with_resize(self):
# gh-162881: an optimistic read from a resized list's old item array
# must not return an object that was never in the list.
# Add the following line before _Py_TryXGetRef in list_get_item_ref:
# nanosleep(&(struct timespec) { .tv_sec = 0, .tv_nsec = 1000000 }, NULL);
class Item:
__slots__ = ("valid", "__weakref__")
def __init__(self, valid):
self.valid = valid
lst = []
stop = Event()
bad = []
retained = []
writer_errors = []
padding = (None,) * 64
def writer():
try:
for _ in range(50_000):
if stop.is_set():
break
lst.clear()
old = Item(True)
old_id = id(old)
lst.append(old)
del old
lst.extend(padding) # Force a new item array.
lst[0] = Item(True) # Release the object in the old array.
candidates = []
for _ in range(128):
candidate = Item(False)
candidates.append(candidate)
if id(candidate) == old_id:
# Make an optimistic incref from the reader possible.
retained.append((candidate, weakref.ref(candidate)))
break
del candidate, candidates
except BaseException as exc:
writer_errors.append(exc)
finally:
stop.set()
with threading_helper.start_threads([Thread(target=writer)],
unlock=stop.set):
while not stop.is_set():
try:
item = lst[0]
except IndexError:
continue
if type(item) is not Item or not item.valid:
bad.append(item)
stop.set()
break
del item # Do not keep the old item alive during the next read.
if writer_errors:
raise writer_errors[0]
self.assertEqual(bad, []) |
Contributor
Author
|
I think the test failure is related to #162963 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After incrementing the reference count of
item, check that the list array is the one from which the reference was fetched. This avoids the race described in the issue, that leads tolist_get_item_refpossibly returning an object that was never in the list.This needs backports for 3.13, 3.14, and 3.15.
list.__getitem__may return an item that was never in the list #162881