diff --git a/proxy/http/handler.py b/proxy/http/handler.py index b8d207d5..26ff8ed1 100644 --- a/proxy/http/handler.py +++ b/proxy/http/handler.py @@ -37,6 +37,8 @@ class HttpProtocolHandler(BaseTcpServerHandler[HttpClientConnection]): Accepts `Client` connection and delegates to HttpProtocolHandlerPlugin. """ + server_teared = False + def __init__(self, *args: Any, **kwargs: Any): super().__init__(*args, **kwargs) self.start_time: float = time.time() @@ -145,15 +147,19 @@ class HttpProtocolHandler(BaseTcpServerHandler[HttpClientConnection]): teardown = await self.plugin.write_to_descriptors(writables) if teardown: return True - # Read from ready to read sockets - teardown = await self.handle_readables(readables) - if teardown: - return True - # Invoke plugin.read_from_descriptors - if self.plugin: - teardown = await self.plugin.read_from_descriptors(readables) + if not self.server_teared: + # Read from ready to read sockets + teardown = await self.handle_readables(readables) if teardown: - return True + self.server_teared = True + # Invoke plugin.read_from_descriptors + if self.plugin: + teardown = await self.plugin.read_from_descriptors(readables) + if teardown: + self.server_teared = True + if self.server_teared and not self.work.has_buffer(): + # Wait until client buffer flushed when server teared down. + return True return False def handle_data(self, data: memoryview) -> Optional[bool]: