@@ -113,13 +113,12 @@ 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
116+ # This flag marks a soon end of the connection.
117+ self .closing = False
118118
119119 # This flag marks the end of the connection.
120120 self .closed = False
121121
122-
123122 async def get (self , decode : bool | None = None ) -> Data :
124123 """
125124 Read the next message.
@@ -142,8 +141,6 @@ async def get(self, decode: bool | None = None) -> Data:
142141 :meth:`get_iter` concurrently.
143142
144143 """
145- if self .cancelling :
146- return
147144 if self .get_in_progress :
148145 raise ConcurrencyError ("get() or get_iter() is already running" )
149146 self .get_in_progress = True
@@ -207,8 +204,6 @@ async def get_iter(self, decode: bool | None = None) -> AsyncIterator[Data]:
207204 :meth:`get_iter` concurrently.
208205
209206 """
210- if self .cancelling :
211- return
212207 if self .get_in_progress :
213208 raise ConcurrencyError ("get() or get_iter() is already running" )
214209 self .get_in_progress = True
@@ -259,13 +254,13 @@ def put(self, frame: Frame) -> None:
259254 EOFError: If the stream of frames has ended.
260255
261256 """
262- if self .cancelling :
263- return
264257 if self .closed :
265258 raise EOFError ("stream of frames ended" )
266259
267260 self .frames .put (frame )
268- self .maybe_pause ()
261+
262+ if not self .closing :
263+ self .maybe_pause ()
269264
270265 def maybe_pause (self ) -> None :
271266 """Pause the writer if queue is above the high water mark."""
@@ -289,6 +284,16 @@ def maybe_resume(self) -> None:
289284 self .paused = False
290285 self .resume ()
291286
287+ def prepare_close (self ) -> None :
288+ """
289+ Prepare to close by ensuring that no more messages will be processed.
290+ """
291+ self .closing = True
292+
293+ # Resuming the writer to avoid deadlocks
294+ if self .paused :
295+ self .resume ()
296+
292297 def close (self ) -> None :
293298 """
294299 End the stream of frames.
0 commit comments