Skip to content

Commit 940bc3c

Browse files
committed
Close the internally opened connection on the controller
This connection is opened to get the model_uuid, and creates its own pinger/receiver tasks etc that need to be gracefully handled before the main model is disconnected at teardown.
1 parent 48570bb commit 940bc3c

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

juju/client/connector.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ async def connect(self, **kwargs):
7373
assert self._connection
7474
self._log_connection = await Connection.connect(**kwargs)
7575
else:
76+
# TODO (cderici): we need to investigate how to reuse/share
77+
# connections between Model & Controller objects
78+
# At least either avoid overwriting self._connection with
79+
# different types of connections (model, controller), or
80+
# have an indication of which type of entity this is
81+
# connected to.
7682
if self._connection:
7783
await self._connection.close()
7884
self._connection = await Connection.connect(**kwargs)
@@ -84,11 +90,11 @@ async def connect(self, **kwargs):
8490
if not self._connection.info['server-version'].startswith(SUPPORTED_MAJOR_VERSION):
8591
raise JujuConnectionError("juju server-version %s not supported" % juju_server_version)
8692

87-
async def disconnect(self):
93+
async def disconnect(self, entity):
8894
"""Shut down the watcher task and close websockets.
8995
"""
9096
if self._connection:
91-
log.debug('Closing model connection')
97+
log.debug(f'Closing {entity} connection')
9298
await self._connection.close()
9399
self._connection = None
94100
if self._log_connection:

juju/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async def disconnect(self):
193193
"""Shut down the watcher task and close websockets.
194194
195195
"""
196-
await self._connector.disconnect()
196+
await self._connector.disconnect(entity='controller')
197197

198198
async def add_credential(self, name=None, credential=None, cloud=None,
199199
owner=None, force=False):

juju/model.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,13 @@ async def watch_received_waiter():
772772
raise self._watcher_task.exception()
773773

774774
if self.info is None:
775-
contr = await self.get_controller()
776-
self._info = await contr.get_model_info(model_name, model_uuid)
777-
log.debug('Got ModelInfo: %s', vars(self.info))
775+
# TODO (cderici): See if this can be optimized away, or at least
776+
# be done lazily (i.e. not everytime after_connect, but whenever
777+
# self.info is needed -- which here can be bypassed if model_uuid
778+
# is known)
779+
async with ConnectedController(self.connection()) as contr:
780+
self._info = await contr.get_model_info(model_name, model_uuid)
781+
log.debug('Got ModelInfo: %s', vars(self.info))
778782

779783
self.uuid = self.info.uuid
780784

@@ -795,8 +799,7 @@ async def disconnect(self):
795799
self._watch_stopping.clear()
796800

797801
if self.is_connected():
798-
log.debug('Closing model connection')
799-
await self._connector.disconnect()
802+
await self._connector.disconnect(entity='Model')
800803
self._info = None
801804

802805
async def add_local_charm_dir(self, charm_dir, series):

0 commit comments

Comments
 (0)