Skip to content

Commit daeb873

Browse files
authored
Merge pull request #1255 from james-garner-canonical/2025-03/fix/websockets-connectionclosed
#1255 #### Description Fixes: #1252 #### QA Steps Test should keep passing. #### Notes & Discussion I wonder if we need to bump our minimum websockets version.
2 parents 71f885b + 416de02 commit daeb873

3 files changed

Lines changed: 10 additions & 14 deletions

File tree

juju/client/connection.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ async def close(self, to_reconnect: bool = False):
368368
await asyncio.gather(*tasks_need_to_be_gathered)
369369
except asyncio.CancelledError:
370370
pass
371-
except websockets.exceptions.ConnectionClosed:
371+
except websockets.ConnectionClosed:
372372
pass
373373

374374
self._pinger_task = None
@@ -380,7 +380,7 @@ async def close(self, to_reconnect: bool = False):
380380

381381
async def _recv(self, request_id: int) -> dict[str, Any]:
382382
if not self.is_open:
383-
raise websockets.exceptions.ConnectionClosedOK(None, None)
383+
raise websockets.ConnectionClosedOK(rcvd=None, sent=None)
384384
try:
385385
return await self.messages.get(request_id)
386386
except GeneratorExit:
@@ -463,7 +463,7 @@ async def _debug_logger(self):
463463
except asyncio.CancelledError:
464464
asyncio.create_task(self.close(), name="Task_Close") # noqa: RUF006
465465
raise
466-
except websockets.exceptions.ConnectionClosed:
466+
except websockets.ConnectionClosed:
467467
log.warning("Debug Logger: Connection closed, reconnecting")
468468
# the reconnect has to be done as a task because the receiver will
469469
# be cancelled by the reconnect and we don't want the reconnect
@@ -489,7 +489,7 @@ async def _receiver(self):
489489
except asyncio.CancelledError:
490490
log.debug("Receiver: Cancelled")
491491
pass
492-
except websockets.exceptions.ConnectionClosed as e:
492+
except websockets.ConnectionClosed as e:
493493
log.warning("Receiver: Connection closed, reconnecting")
494494
await self.messages.put_all(e)
495495
# the reconnect has to be done as a task because the receiver will
@@ -530,7 +530,7 @@ async def _do_ping():
530530
except asyncio.CancelledError:
531531
log.debug("Pinger: Cancelled")
532532
pass
533-
except websockets.exceptions.ConnectionClosed:
533+
except websockets.ConnectionClosed:
534534
# The connection has closed - we can't do anything
535535
# more until the connection is restarted.
536536
log.debug("ping failed because of closed connection")
@@ -568,11 +568,8 @@ async def rpc(
568568
for attempt in range(3):
569569
if self.monitor.status == Monitor.DISCONNECTED:
570570
# closed cleanly; shouldn't try to reconnect
571-
raise websockets.exceptions.ConnectionClosed(
572-
websockets.frames.Close(
573-
websockets.frames.CloseCode.NORMAL_CLOSURE, "websocket closed"
574-
)
575-
)
571+
# we're trying to use a reference to a monitor that was cleanly closed earlier
572+
raise websockets.ConnectionClosedOK(rcvd=None, sent=None)
576573
try:
577574
await self._ws.send(outgoing)
578575
break

juju/model/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ async def remove_application(
11581158
async def block_until(self, *conditions, timeout=None, wait_period=0.5):
11591159
"""Return only after all conditions are true.
11601160
1161-
Raises `websockets.ConnectionClosed` if disconnected.
1161+
Raises `websockets.ConnectionClosedError` if disconnected.
11621162
"""
11631163

11641164
def _disconnected():
@@ -1169,7 +1169,7 @@ def done():
11691169

11701170
await utils.block_until(done, timeout=timeout, wait_period=wait_period)
11711171
if _disconnected():
1172-
raise websockets.ConnectionClosed(1006, "no reason")
1172+
raise websockets.ConnectionClosedError(rcvd=None, sent=None)
11731173

11741174
@property
11751175
def tag(self):

tests/unit/test_connection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import pytest
1010
import websockets
11-
from websockets.exceptions import ConnectionClosed
1211

1312
from juju.client.connection import Connection
1413
from juju.errors import JujuRedirectException
@@ -26,7 +25,7 @@ async def send(self, message):
2625
async def recv(self):
2726
if not self.responses:
2827
await asyncio.sleep(1) # delay to give test time to finish
29-
raise ConnectionClosed(0, "ran out of responses")
28+
raise websockets.ConnectionClosedOK(rvcd=None, sent=None)
3029
return json.dumps(self.responses.popleft())
3130

3231
async def close(self):

0 commit comments

Comments
 (0)