Repository navigation
Update _cluster_info dict in init - #5305
Conversation
There was a problem hiding this comment.
Thanks @jacobtomlinson! I'm probably just missing something, but it's not clear to me why the current code in main is problematic, regardless of what order Cluster.__init__(...) is called in a subclass' __init__ method. Maybe a subclass' __init__ method mutates or inspects self._cluster_info? Is there a test we could add which illustrates the problematic behavior?
EDIT: To be clear, the changes here seem reasonable, I think I'm just missing a piece related to why they're needed
Yes this is the reason. The |
jrbourbeau
left a comment
There was a problem hiding this comment.
Whoops, I forgot to merge this in. Thanks @jacobtomlinson!
| async def test_cluster_info(): | ||
| class FooCluster(Cluster): | ||
| def __init__(self): | ||
| self._cluster_info["foo"] = "bar" |
There was a problem hiding this comment.
this mutation now applies to all other subclasses of Cluster - and isn't threadsafe:
>>> from distributed.deploy import Cluster
>>> class FooCluster(Cluster):
... def __init__(self):
... self._cluster_info["foo"] = "bar"
... super().__init__(asynchronous=False)
...
>>> c = FooCluster()
>>> c._cluster_info
{'name': 'c74e5491', 'type': '__main__.FooCluster', 'foo': 'bar'}
>>> FooCluster._cluster_info
{'foo': 'bar'}
>>> Cluster._cluster_info
{'foo': 'bar'}
>>>
The
_cluster_infodict is designed to be populated during__init__. But someFooClustersubclass implementations callsuper().__init__()first, and some last.For instance all
SpecClusterimplementations require the super init to be called last because in sync mode the super starts the event loop and enters the_startmethod during init.This change avoids the
_cluster_infofrom being overwritten in either scenario.