From 255bf5c4bcacbec9527aa0666852b3411eeee09b Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:29:07 +0000 Subject: [PATCH] [PR #9795/fcb790b1 backport][3.11] Switch WebSocketWriter to use `removesuffix` to drop compression trailer (#9805) Co-authored-by: J. Nick Koston --- aiohttp/_websocket/writer.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aiohttp/_websocket/writer.py b/aiohttp/_websocket/writer.py index 4935690f339..fc2cf32b934 100644 --- a/aiohttp/_websocket/writer.py +++ b/aiohttp/_websocket/writer.py @@ -92,16 +92,16 @@ async def send_frame( self._compressobj = self._make_compress_obj(self.compress) compressobj = self._compressobj - message = await compressobj.compress(message) + message = ( + await compressobj.compress(message) + + compressobj.flush( + zlib.Z_FULL_FLUSH if self.notakeover else zlib.Z_SYNC_FLUSH + ) + ).removesuffix(WS_DEFLATE_TRAILING) # Its critical that we do not return control to the event # loop until we have finished sending all the compressed # data. Otherwise we could end up mixing compressed frames # if there are multiple coroutines compressing data. - message += compressobj.flush( - zlib.Z_FULL_FLUSH if self.notakeover else zlib.Z_SYNC_FLUSH - ) - if message.endswith(WS_DEFLATE_TRAILING): - message = message[:-4] msg_length = len(message)