diff --git a/scrapy/core/downloader/handlers/_httpx.py b/scrapy/core/downloader/handlers/_httpx.py index da39e1bbc..fa712d119 100644 --- a/scrapy/core/downloader/handlers/_httpx.py +++ b/scrapy/core/downloader/handlers/_httpx.py @@ -95,8 +95,12 @@ class HttpxDownloadHandler(_Base): # hard limit on simultaneous connections (None for no limit, which # is what a CONCURRENT_CONNECTIONS_PER_HANDLER of 0 means) max_connections=self._pool_size_total or None, - # total number of idle connections in the pool (extra ones are closed) - max_keepalive_connections=self._pool_size_total or None, + # total number of idle connections in the pool (extra ones are + # closed); keeping none of them is how keepalive gets disabled, + # since expiry alone is subject to the resolution of the clock + max_keepalive_connections=( + (self._pool_size_total or None) if self._keepalive_timeout else 0 + ), keepalive_expiry=self._keepalive_timeout, )