Skip to content

Commit ff0310b

Browse files
committed
Avoid gathering all tasks when reconnect-ing
1 parent c29a3f2 commit ff0310b

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

juju/client/connection.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def status(self):
218218
if connection.is_debug_log_connection:
219219
stopped = connection._debug_log_task.cancelled()
220220
else:
221-
stopped = connection._receiver_task.cancelled()
221+
stopped = connection._receiver_task is not None and \
222+
connection._receiver_task.cancelled()
222223

223224
if stopped or not connection._ws.open:
224225
return self.ERROR
@@ -451,17 +452,17 @@ async def close(self, to_reconnect=False):
451452
if self._ws and not self._ws.closed:
452453
await self._ws.close()
453454

454-
try:
455-
log.debug('Gathering all tasks for connection close')
455+
if not to_reconnect:
456+
try:
457+
log.debug('Gathering all tasks for connection close')
456458

457-
# Avoid gathering the current task
458-
tasks_need_to_be_gathered = [task for task in jasyncio.all_tasks()
459-
if task != jasyncio.current_task()]
460-
await jasyncio.gather(*tasks_need_to_be_gathered)
461-
except jasyncio.CancelledError:
462-
pass
463-
except websockets.exceptions.ConnectionClosed:
464-
pass
459+
# Avoid gathering the current task
460+
tasks_need_to_be_gathered = [task for task in jasyncio.all_tasks() if task != jasyncio.current_task()]
461+
await jasyncio.gather(*tasks_need_to_be_gathered)
462+
except jasyncio.CancelledError:
463+
pass
464+
except websockets.exceptions.ConnectionClosed:
465+
pass
465466

466467
self._pinger_task = None
467468
self._receiver_task = None

tests/integration/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def test_monitor(event_loop):
3131
assert conn.monitor.status == 'connected'
3232
await conn.close()
3333

34-
assert conn.monitor.status == 'disconnected'
34+
assert conn.monitor.status == 'disconnecting'
3535

3636

3737
@base.bootstrapped

0 commit comments

Comments
 (0)