Skip to content

Commit

Permalink
[PR #9667/5b654d57 backport][3.11] Simplify close logic in client…
Browse files Browse the repository at this point in the history
… ``WebSocketResponse`` (#9668)

Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
patchback[bot] and bdraco authored Nov 4, 2024
1 parent aa42f60 commit 262fb12
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,30 @@ async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bo
self._reader.feed_data(WS_CLOSING_MESSAGE, 0)
await self._close_wait

if not self._closed:
self._set_closed()
if self._closed:
return False

self._set_closed()
try:
await self._writer.close(code, message)
except asyncio.CancelledError:
self._close_code = WSCloseCode.ABNORMAL_CLOSURE
self._response.close()
raise
except Exception as exc:
self._close_code = WSCloseCode.ABNORMAL_CLOSURE
self._exception = exc
self._response.close()
return True

if self._close_code:
self._response.close()
return True

while True:
try:
await self._writer.close(code, message)
async with async_timeout.timeout(self._timeout.ws_close):
msg = await self._reader.read()
except asyncio.CancelledError:
self._close_code = WSCloseCode.ABNORMAL_CLOSURE
self._response.close()
Expand All @@ -281,31 +301,11 @@ async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bo
self._response.close()
return True

if self._close_code:
if msg.type is WSMsgType.CLOSE:
self._close_code = msg.data
self._response.close()
return True

while True:
try:
async with async_timeout.timeout(self._timeout.ws_close):
msg = await self._reader.read()
except asyncio.CancelledError:
self._close_code = WSCloseCode.ABNORMAL_CLOSURE
self._response.close()
raise
except Exception as exc:
self._close_code = WSCloseCode.ABNORMAL_CLOSURE
self._exception = exc
self._response.close()
return True

if msg.type is WSMsgType.CLOSE:
self._close_code = msg.data
self._response.close()
return True
else:
return False

async def receive(self, timeout: Optional[float] = None) -> WSMessage:
receive_timeout = timeout or self._timeout.ws_receive

Expand Down

0 comments on commit 262fb12

Please sign in to comment.