diff --git a/sanic/server/protocols/http_protocol.py b/sanic/server/protocols/http_protocol.py index 6e30fc0fbd..7608a4f931 100644 --- a/sanic/server/protocols/http_protocol.py +++ b/sanic/server/protocols/http_protocol.py @@ -109,6 +109,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta): "_http", "_exception", "recv_buffer", + "_callback_check_timeouts", ) def __init__( @@ -135,6 +136,7 @@ def __init__( if "requests_count" not in self.state: self.state["requests_count"] = 0 self._exception = None + self._callback_check_timeouts = None async def connection_task(self): # no cov """ @@ -214,7 +216,10 @@ def check_timeouts(self): ) / 2 ) - self.loop.call_later(max(0.1, interval), self.check_timeouts) + _interval = max(0.1, interval) + self._callback_check_timeouts = self.loop.call_later( + _interval, self.check_timeouts + ) return cancel_msg_args = () if sys.version_info >= (3, 9): @@ -223,6 +228,19 @@ def check_timeouts(self): except Exception: error_logger.exception("protocol.check_timeouts") + def close(self, timeout: Optional[float] = None): + """ + Requires to prevent checking timeouts for closed connections + """ + if timeout is not None: + super().close(timeout=timeout) + return + if self._callback_check_timeouts: + self._callback_check_timeouts.cancel() + if self.transport: + self.transport.close() + self.abort() + async def send(self, data): # no cov """ Writes HTTP data with backpressure control.