Skip to content

Commit 0215b10

Browse files
Resolve "Default max_queue blocks websocket cancellation with high traffic"
1 parent 59d4dcf commit 0215b10

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/websockets/asyncio/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ async def send_context(
913913
if wait_for_close:
914914
try:
915915
async with asyncio_timeout_at(self.close_deadline):
916+
self.recv_messages.cancelling = True
917+
if self.recv_messages.paused:
918+
self.recv_messages.resume()
916919
await asyncio.shield(self.connection_lost_waiter)
917920
except TimeoutError:
918921
# There's no risk to overwrite another error because

src/websockets/asyncio/messages.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ def __init__( # pragma: no cover
113113
# This flag prevents concurrent calls to get() by user code.
114114
self.get_in_progress = False
115115

116+
# This flag marks a soon cancellation
117+
self.cancelling = False
118+
116119
# This flag marks the end of the connection.
117120
self.closed = False
118121

122+
119123
async def get(self, decode: bool | None = None) -> Data:
120124
"""
121125
Read the next message.
@@ -138,6 +142,8 @@ async def get(self, decode: bool | None = None) -> Data:
138142
:meth:`get_iter` concurrently.
139143
140144
"""
145+
if self.cancelling:
146+
return
141147
if self.get_in_progress:
142148
raise ConcurrencyError("get() or get_iter() is already running")
143149
self.get_in_progress = True
@@ -201,6 +207,8 @@ async def get_iter(self, decode: bool | None = None) -> AsyncIterator[Data]:
201207
:meth:`get_iter` concurrently.
202208
203209
"""
210+
if self.cancelling:
211+
return
204212
if self.get_in_progress:
205213
raise ConcurrencyError("get() or get_iter() is already running")
206214
self.get_in_progress = True
@@ -251,6 +259,8 @@ def put(self, frame: Frame) -> None:
251259
EOFError: If the stream of frames has ended.
252260
253261
"""
262+
if self.cancelling:
263+
return
254264
if self.closed:
255265
raise EOFError("stream of frames ended")
256266

@@ -283,7 +293,7 @@ def close(self) -> None:
283293
"""
284294
End the stream of frames.
285295
286-
Callling :meth:`close` concurrently with :meth:`get`, :meth:`get_iter`,
296+
Calling :meth:`close` concurrently with :meth:`get`, :meth:`get_iter`,
287297
or :meth:`put` is safe. They will raise :exc:`EOFError`.
288298
289299
"""

0 commit comments

Comments
 (0)