diff --git a/CHANGES.rst b/CHANGES.rst index fcea8e130bf..9b42eacae09 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -23,7 +23,7 @@ Changes - Make sure cleanup signal is sent if startup signal has been sent #1959 -- +- Fixed server keep-alive handler, could cause 100% cpu utilization #1955 - diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 303c0352f4a..4f1f7df2e89 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -570,6 +570,10 @@ def strtime(self): def loop_time(self): return self._loop_time + @property + def interval(self): + return self._interval + def _weakref_handle(info): ref, name = info diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index 38d30c39342..59c10116d2d 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -369,8 +369,9 @@ def _process_keepalive(self): # all handlers in idle state if len(self._request_handlers) == len(self._waiters): + # time_service.loop_time is ceiled to 1.0, so we check 2 intervals now = self._time_service.loop_time - if now + 1.0 > next: + if (now + self._time_service.interval * 2) > next: self.force_close(send_last_heartbeat=True) return