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
Conversation
| @@ -90,7 +90,13 @@ def clear(self): | |||
| self.data.clear() | |||
|
|
|||
| def copy(self): | |||
| return self.__class__(self) | |||
| new = WeakSet() | |||
There was a problem hiding this comment.
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?
| new = WeakSet() | |
| if self._pending_removals: | |
| self._commit_removals() | |
| new = WeakSet() |
There was a problem hiding this comment.
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:
- there's potentially less state to copy that way;
- since you're not using
_IterationGuardanymore, the previous indirect call to_commit_removals()was removed.
| @@ -90,7 +90,13 @@ def clear(self): | |||
| self.data.clear() | |||
|
|
|||
| def copy(self): | |||
| return self.__class__(self) | |||
| new = WeakSet() | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
| new = WeakSet() | |
| new = self.__class__() |
but then it would be different to the other weak collections, that all use the explicit class name
There was a problem hiding this comment.
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__?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| return self.__class__(self) | ||
| new = WeakSet() | ||
| add = new.add | ||
| for key in self.data.copy(): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()} |
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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(): | |||
There was a problem hiding this comment.
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.
|
This PR is stale because it has been open for 30 days with no activity. |
|
Closing, see my comment on the issue. |


https://bugs.python.org/issue45276