From 68b16ec940e994cc12ea5ccbcbcbe32c944582a4 Mon Sep 17 00:00:00 2001 From: Peter Seiderer Date: Thu, 8 Oct 2026 12:10:27 +0200 Subject: [PATCH] Fix asyncio messages handling for CancelledError. Fix asyncio messages handling for CancelledError hitting the following assert: File "/usr/lib/python3.14/site-packages/websockets/asyncio/messages.py", line 60, in reset assert not self.queue, "cannot reset() while queue isn't empty" Put back the already received frames although into an non empty queue (at the right places). Bug was introduced by commit 1387c97 ("Rewrite sync Assembler to improve performance."). Fix #1773 Signed-off-by: Peter Seiderer --- Changes v1 -> v2: - fix static typing (mypy) --- src/websockets/asyncio/messages.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/websockets/asyncio/messages.py b/src/websockets/asyncio/messages.py index 1d71d97bf..bacb8139a 100644 --- a/src/websockets/asyncio/messages.py +++ b/src/websockets/asyncio/messages.py @@ -4,7 +4,7 @@ import codecs import collections from collections.abc import AsyncIterator, Iterable -from typing import Any, Callable, Generic, Literal, TypeVar, overload +from typing import Any, Callable, Generic, List, Literal, TypeVar, overload from ..exceptions import ConcurrencyError from ..frames import BINARY, CONT, TEXT, Frame @@ -54,10 +54,11 @@ async def get(self, block: bool = True) -> T: self.get_waiter = None return self.queue.popleft() - def reset(self, items: Iterable[T]) -> None: - """Put back items into an empty, idle queue.""" + def reset(self, items: List[T]) -> None: + """Put back items into the idle queue.""" assert self.get_waiter is None, "cannot reset() while get() is running" - assert not self.queue, "cannot reset() while queue isn't empty" + items.extend(self.queue) + self.queue.clear() self.queue.extend(items) def abort(self) -> None: