@@ -148,6 +148,7 @@ class RequestHandler(BaseProtocol):
148
148
"_lingering_time" ,
149
149
"_messages" ,
150
150
"_message_tail" ,
151
+ "_handler_waiter" ,
151
152
"_waiter" ,
152
153
"_task_handler" ,
153
154
"_upgrade" ,
@@ -204,6 +205,7 @@ def __init__(
204
205
self ._message_tail = b""
205
206
206
207
self ._waiter : Optional [asyncio .Future [None ]] = None
208
+ self ._handler_waiter : Optional [asyncio .Future [None ]] = None
207
209
self ._task_handler : Optional [asyncio .Task [None ]] = None
208
210
209
211
self ._upgrade = False
@@ -262,11 +264,11 @@ async def shutdown(self, timeout: Optional[float] = 15.0) -> None:
262
264
if self ._waiter :
263
265
self ._waiter .cancel ()
264
266
265
- # Wait for graceful disconnection
266
- if self ._current_request is not None :
267
+ # Wait for graceful handler completion
268
+ if self ._handler_waiter is not None :
267
269
with suppress (asyncio .CancelledError , asyncio .TimeoutError ):
268
270
async with ceil_timeout (timeout ):
269
- await self ._current_request . wait_for_disconnection ()
271
+ await self ._handler_waiter
270
272
# Then cancel handler and wait
271
273
with suppress (asyncio .CancelledError , asyncio .TimeoutError ):
272
274
async with ceil_timeout (timeout ):
@@ -450,6 +452,7 @@ async def _handle_request(
450
452
start_time : float ,
451
453
request_handler : Callable [[BaseRequest ], Awaitable [StreamResponse ]],
452
454
) -> Tuple [StreamResponse , bool ]:
455
+ self ._handler_waiter = self ._loop .create_future ()
453
456
try :
454
457
try :
455
458
self ._current_request = request
@@ -479,6 +482,8 @@ async def _handle_request(
479
482
)
480
483
481
484
reset = await self .finish_response (request , resp , start_time )
485
+ finally :
486
+ self ._handler_waiter .set_result (None )
482
487
483
488
return resp , reset
484
489
0 commit comments