-
-
Notifications
You must be signed in to change notification settings - Fork 719
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
Track worker_state_machine.TaskState
instances
#6525
Changes from 11 commits
278f5c9
972beec
5476c55
51bab47
f566d4b
d5ac950
1875060
be6db26
ccac9cf
8e73145
748dc48
b2f1efc
9a0c805
1edbb34
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||
from __future__ import annotations | ||||||||||||
|
||||||||||||
import sys | ||||||||||||
import weakref | ||||||||||||
from collections.abc import Collection, Container | ||||||||||||
from copy import copy | ||||||||||||
from dataclasses import dataclass, field | ||||||||||||
|
@@ -231,20 +232,23 @@ class TaskState: | |||||||||||
#: True if the task is in memory or erred; False otherwise | ||||||||||||
done: bool = False | ||||||||||||
|
||||||||||||
_instances: ClassVar[weakref.WeakSet[TaskState]] = weakref.WeakSet() | ||||||||||||
|
||||||||||||
# Support for weakrefs to a class with __slots__ | ||||||||||||
__weakref__: Any = field(init=False) | ||||||||||||
|
||||||||||||
def __post_init__(self): | ||||||||||||
TaskState._instances.add(self) | ||||||||||||
Comment on lines
+271
to
+272
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 doesn't work on unpickle.
Suggested change
+ unit test for pickle/unpickle round trip 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. We're not pickling TaskState objects anywhere, are we? 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. We aren't today, but that's not an excuse to have it future-proofed. Namely it would make a lot of sense to pickle dump our new 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. Well, the "excuse" is scope creep. Using new as in def __new__(cls, *args, **kwargs):
inst = object.__new__(cls)
TaskState._instances.add(inst)
return inst does not work because we're defining the hash function as the hash of the key, i.e. we can only add fully initialized TaskState objects to the weakref. Apart from this, we actually can (un-)pickle this class but will simply not add the instance to this weakset. For the only purpose of dumping this (like in our cluster dump) this is absolutely sufficient. |
||||||||||||
|
||||||||||||
def __repr__(self) -> str: | ||||||||||||
return f"<TaskState {self.key!r} {self.state}>" | ||||||||||||
|
||||||||||||
def __eq__(self, other: object) -> bool: | ||||||||||||
if not isinstance(other, TaskState) or other.key != self.key: | ||||||||||||
return False | ||||||||||||
# When a task transitions to forgotten and exits Worker.tasks, it should be | ||||||||||||
# immediately dereferenced. If the same task is recreated later on on the | ||||||||||||
# worker, we should not have to deal with its previous incarnation lingering. | ||||||||||||
assert other is self | ||||||||||||
fjetter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
return True | ||||||||||||
# A task may be forgotten and a new TaskState object with the same key may be created in | ||||||||||||
# its place later on. In the Worker state, you should never have multiple TaskState objects with | ||||||||||||
# the same key. We can't assert it here however, as this comparison is also used in WeakSets | ||||||||||||
# for instance tracking purposes. | ||||||||||||
return other is self | ||||||||||||
|
||||||||||||
def __hash__(self) -> int: | ||||||||||||
return hash(self.key) | ||||||||||||
|
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.
You should add the
clean
ctxmanager or just theclean_instances
ctxmanager. I don't think this is applied automatically and this test would otherwise be fragile and dependent on test order