Visitar URL original
bpo-45276: make weak collection's copy() atomic and use this feature to avoid race conditions in asyncio and threading by graingert · Pull Request #28541 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223071130/https://github.com/python/cpython/pull/28541
Skip to content
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

bpo-45276: make weak collection's copy() atomic and use this feature to avoid race conditions in asyncio and threading #28541

Closed
wants to merge 4 commits into from

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Sep 24, 2021 •

@@ -90,7 +90,13 @@ def clear(self):
self.data.clear()

def copy(self):
return self.__class__(self)
new = WeakSet()
Copy link
Contributor Author

@graingert graingert Sep 24, 2021 •

Choose a reason for hiding this comment

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

self.__class__(self) doesn't call _commit_removals like the Weak Dictionary versions do, maybe it should have?

Suggested change
new = WeakSet()
if self._pending_removals:
self._commit_removals()
new = WeakSet()

Copy link
Contributor

@ambv ambv Sep 29, 2021 •

Choose a reason for hiding this comment

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

Yes, that would be good because:

  1. there's potentially less state to copy that way;
  2. since you're not using _IterationGuard anymore, the previous indirect call to _commit_removals() was removed.

Lib/asyncio/tasks.py Outdated Show resolved Hide resolved
Copy link
Member

@vstinner vstinner left a comment

@pitrou @serhiy-storchaka: You may be interested to review this interesting fix.

@@ -90,7 +90,13 @@ def clear(self):
self.data.clear()

def copy(self):
return self.__class__(self)
new = WeakSet()
Copy link
Member

@vstinner vstinner Sep 24, 2021

Choose a reason for hiding this comment

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

I'm not sure that it works as expected for subclasses.

Copy link
Contributor Author

@graingert graingert Sep 24, 2021 •

Choose a reason for hiding this comment

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

I could change this to be:

Suggested change
new = WeakSet()
new = self.__class__()

but then it would be different to the other weak collections, that all use the explicit class name

Copy link
Contributor Author

@graingert graingert Sep 24, 2021

Choose a reason for hiding this comment

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

would it be better to use return self.__class__(self) in all the weak collections and then in .update() special case instances of __class__?

Copy link
Contributor

@ambv ambv Sep 29, 2021

Choose a reason for hiding this comment

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

Other WeakRef collections predate WeakSet. The latter's been added in 3b8cb17 and used self.__class__(self) for copying ever since. It's invalid now to change this to WeakSet() in the interest of "consistency".

Changing other weak collections to use self.__class__(self) is also technically a backwards incompatible change but with less opportunity for breakage. That we could definitely consider.

Copy link
Contributor Author

@graingert graingert Sep 29, 2021

Choose a reason for hiding this comment

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

Ok I'll switch them over and special case update

Lib/threading.py Show resolved Hide resolved
return self.__class__(self)
new = WeakSet()
add = new.add
for key in self.data.copy():
Copy link
Contributor

@ambv ambv Sep 29, 2021

Choose a reason for hiding this comment

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

This relies on dict.copy atomicity which was achieved with BPO-31179 but AFAICT the feature isn't specified explicitly anywhere so it should rather be treated as an implementation detail as it might have regressed since.

Copy link
Contributor Author

@graingert graingert Sep 29, 2021

Choose a reason for hiding this comment

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

Do you mean for me to add documentation for atomic dict.copy?

Copy link
Member

@JelleZijlstra JelleZijlstra Feb 1, 2022

Choose a reason for hiding this comment

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

Pretty sure he wants you to not have code that relies on dict.copy being atomic, because its atomicity is not guaranteed.

i = 0
while True:
try:
tasks = list(_all_tasks)
except RuntimeError:
i += 1
if i >= 1000:
raise
else:
break
return {t for t in tasks
# thread while we do so. Therefore we copy it prior to filtering.
return {t for t in _all_tasks.copy()
if futures._get_loop(t) is loop and not t.done()}
Copy link
Contributor

@ambv ambv Sep 29, 2021

Choose a reason for hiding this comment

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

I'm happy to see this finally addressed. Note: https://bugs.python.org/issue36607#msg345380

It's a behavioral change though: an unintentional feature of the previous implementation was that, due to restarting on mutation, it returned a set that was "as fresh as possible". Your version might be a tiny bit off. However, as discussion on the issue I linked demonstrates, the previous behavior was only working for a subset of possible cases so in the end I think the atomicity is better.

o = wr()
if o is not None:
new[key] = o
for key, wr in self.data.copy().items():
Copy link
Contributor

@ambv ambv Sep 29, 2021

Choose a reason for hiding this comment

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

Similarly, I'm not sure we can rely on dict.copy being atomic.

@@ -419,11 +418,10 @@ def __setitem__(self, key, value):

def copy(self):
new = WeakKeyDictionary()
with _IterationGuard(self):
for key, value in self.data.items():
Copy link
Contributor

@ambv ambv Sep 29, 2021

Choose a reason for hiding this comment

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

Similarly, I'm not sure we can rely on dict.copy being atomic.

@github-actions
Copy link

github-actions bot commented Oct 30, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Oct 30, 2021
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Ran test suite plus asyncio.
Looks ok.

@kumaraditya303
Copy link
Contributor

kumaraditya303 commented Dec 2, 2022

Closing, see my comment on the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants