Skip to content

Commit

Permalink
Merge pull request #990 from cderici/websocket-connection-closed
Browse files Browse the repository at this point in the history
#990

#### Description

This fixes a bug in connection where we pass close code and reason directly into the `websockets.exception.ConnectionClosed`, where it needs a Close frame that contains those.

Fixes #989


#### QA Steps

This doesn't affect any normal behavior, as this is sort of a safeguard against races. The only two times we raise this explicit exception is when the `MONITOR` is closed but either the receiver task is running still or someone makes an rpc call, which shouldn't happen anyways. So manually checking the arguments by the [doc](https://websockets.readthedocs.io/en/stable/reference/exceptions.html#websockets.exceptions.ConnectionClosed) is sufficient.

All CI tests need to pass.

#### Notes & Discussion

Needs to be forward ported into 3.x.
  • Loading branch information
jujubot authored Dec 5, 2023
2 parents 95da9a1 + 93cd118 commit 5711ade
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions juju/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ async def close(self, to_reconnect=False):

async def _recv(self, request_id):
if not self.is_open:
raise websockets.exceptions.ConnectionClosed(0, 'websocket closed')
raise websockets.exceptions.ConnectionClosed(
websockets.frames.Close(websockets.frames.CloseCode.NORMAL_CLOSURE,
'websocket closed'))
try:
return await self.messages.get(request_id)
except GeneratorExit:
Expand Down Expand Up @@ -604,7 +606,8 @@ async def rpc(self, msg, encoder=None):
if self.monitor.status == Monitor.DISCONNECTED:
# closed cleanly; shouldn't try to reconnect
raise websockets.exceptions.ConnectionClosed(
0, 'websocket closed')
websockets.frames.Close(websockets.frames.CloseCode.NORMAL_CLOSURE,
'websocket closed'))
try:
await self._ws.send(outgoing)
break
Expand Down

0 comments on commit 5711ade

Please sign in to comment.