Backtestsscrapyrun-20261025-211112#5046

Issue #5046 Merged fix PR #7896 Run 25 Oct 2026 · run-20261025-211112 scrapy/scrapy

H2DownloadHandler: bytes_received signal

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches thread the crawler down to the Stream and send bytes_received/headers_received via check_stop_download, handle the HEADERS/DATA-with-END_STREAM case by tolerating StreamClosedError on reset, and build the partial response via make_response(stop_download=...). The decisive difference is flow control. In Patch A, receive_data returns early after a StopDownload without acknowledging the frame's flow_controlled_length, so every stopped download leaks connection-level window; the independent test test_connection_usable_after_stop_download shows the consequence directly: after several stopped downloads on one HTTP/2 connection the next request times out (DownloadTimeoutError) under Patch A but passes under Patch B. That is exactly the production scenario the issue implies (a spider stopping downloads from bytes_received during a crawl), so Patch A leaves the feature unusable beyond a few requests per connection. Patch B explicitly calls acknowledge_received_data before stopping, uses ErrorCodes.CANCEL (the semantically correct code for a client-abandoned stream), and also guards window_updated against late WINDOW_UPDATE frames for streams already removed locally. On tests, both un-skip the shared crawler-based StopDownload tests, but Patch B additionally adds targeted tests for the END_STREAM edge cases, for connection reuse after a stop, and for the window_updated guard; Patch A adds none. On scope/craft, Patch B keeps crawler as an optional keyword argument so existing H2ConnectionPool/H2ClientFactory/H2ClientProtocol callers (including the existing protocol tests) keep working, whereas Patch A changes the positional settings parameter to crawler across three classes, which is unnecessary churn for the ask. Patch B also passes body_length=-1 for unknown length, consistent with the HTTP/1.1 handler, and adds a news entry. Patch A's only advantage is slightly less code, which does not offset the real correctness defect.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

10m 2s

Wall-clock time in the container

Full test suite

No new failures

0 passed · 190 skipped · 190 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

258vs 123 merged

Changed lines, ours vs merged

(Edit) related to #5055

The two patches

Our patch+223 −35
docs/news.rstindex a5cc6c723..4bf9adf01 100644@@ -25,6 +25,15 @@ Backward-incompatible changes      (:issue:`6585`, :issue:`7731`) +New features+~~~~~~~~~~~~++-   :class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler` now+    sends the :signal:`bytes_received` and :signal:`headers_received` signals+    and supports stopping downloads with+    :exc:`~scrapy.exceptions.StopDownload`.+    (:issue:`5046`)+ .. _release-2.17.0:  Scrapy 2.17.0 (2026-07-07)docs/topics/download-handlers.rstindex e0501c169..94e75ab6f 100644@@ -203,9 +203,6 @@ Other limitations: -   IPv6 support requires setting :setting:`TWISTED_DNS_RESOLVER`     to ``scrapy.resolver.CachingHostnameResolver``. --   No support for the :signal:`bytes_received` and :signal:`headers_received`-    signals.- Known limitations of the HTTP/2 support:  -   No support for HTTP/2 Cleartext (h2c), since no major browser supportsscrapy/core/downloader/handlers/http2.pyindex f60c58d1b..ba99f9ca1 100644@@ -40,7 +40,7 @@ class H2DownloadHandler(BaseDownloadHandler):          from twisted.internet import reactor -        self._pool = H2ConnectionPool(reactor, crawler.settings)+        self._pool = H2ConnectionPool(reactor, crawler.settings, crawler=crawler)         self._context_factory = _load_context_factory_from_settings(crawler)         self._bind_address = crawler.settings.get("DOWNLOAD_BIND_ADDRESS") scrapy/core/http2/agent.pyindex aa55e29a0..771c71ebb 100644@@ -21,6 +21,7 @@ if TYPE_CHECKING:     from twisted.internet.base import ReactorBase     from twisted.internet.endpoints import HostnameEndpoint +    from scrapy.crawler import Crawler     from scrapy.http import Request, Response     from scrapy.settings import Settings     from scrapy.spiders import Spider@@ -30,9 +31,16 @@ ConnectionKeyT = tuple[bytes, bytes, int]   class H2ConnectionPool:-    def __init__(self, reactor: ReactorBase, settings: Settings) -> None:+    def __init__(+        self,+        reactor: ReactorBase,+        settings: Settings,+        *,+        crawler: Crawler | None = None,+    ) -> None:         self._reactor = reactor         self.settings = settings+        self._crawler = crawler          # Store a dictionary which is used to get the respective         # H2ClientProtocolInstance using the  key as Tuple(scheme, hostname, port)@@ -80,6 +88,7 @@ class H2ConnectionPool:             self.settings,             conn_lost_deferred,             tls_verbose_logging=self._tls_verbose_logging,+            crawler=self._crawler,         )         conn_d = endpoint.connect(factory)         conn_d.addCallback(self.put_connection, key)scrapy/core/http2/protocol.pyindex 7136e829e..9a7f49463 100644@@ -44,6 +44,7 @@ if TYPE_CHECKING:     from twisted.python.failure import Failure     from twisted.web.client import URI +    from scrapy.crawler import Crawler     from scrapy.settings import Settings     from scrapy.spiders import Spider @@ -94,6 +95,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):         conn_lost_deferred: Deferred[list[BaseException]],         *,         tls_verbose_logging: bool = False,+        crawler: Crawler | None = None,     ) -> None:         """         Arguments:@@ -104,9 +106,12 @@ class H2ClientProtocol(Protocol, TimeoutMixin):             conn_lost_deferred -- Deferred that fires with the list of underlying exceptions to notify                 that connection was lost             tls_verbose_logging -- Whether to log TLS details+            crawler -- Crawler used to send the bytes_received and+                headers_received signals for the streams of this connection         """         self._conn_lost_deferred: Deferred[list[BaseException]] = conn_lost_deferred         self._tls_verbose_logging: bool = tls_verbose_logging+        self._crawler: Crawler | None = crawler          config = H2Configuration(client_side=True, header_encoding="utf-8")         self.conn = H2Connection(config=config)@@ -208,6 +213,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):             stream_id=next(self._stream_id_generator),             request=request,             protocol=self,+            crawler=self._crawler,             download_maxsize=getattr(                 spider, "download_maxsize", self.metadata["default_download_maxsize"]             ),@@ -449,7 +455,12 @@ class H2ClientProtocol(Protocol, TimeoutMixin):      def window_updated(self, event: WindowUpdated) -> None:         if event.stream_id != 0:-            self.streams[event.stream_id].receive_window_update()+            try:+                stream = self.streams[event.stream_id]+            except KeyError:+                pass  # The stream was already closed locally, e.g. download stopped+            else:+                stream.receive_window_update()         else:             # Send leftover data for all the streams             for stream in self.streams.values():@@ -465,11 +476,13 @@ class H2ClientFactory(Factory):         conn_lost_deferred: Deferred[list[BaseException]],         *,         tls_verbose_logging: bool = False,+        crawler: Crawler | None = None,     ) -> None:         self.uri = uri         self.settings = settings         self.conn_lost_deferred = conn_lost_deferred         self.tls_verbose_logging = tls_verbose_logging+        self.crawler = crawler      def buildProtocol(self, addr: IAddress) -> H2ClientProtocol:         return H2ClientProtocol(@@ -477,6 +490,7 @@ class H2ClientFactory(Factory):             self.settings,             self.conn_lost_deferred,             tls_verbose_logging=self.tls_verbose_logging,+            crawler=self.crawler,         )      def acceptableProtocols(self) -> list[bytes]:scrapy/core/http2/stream.pyindex c6226bbca..0e9125f79 100644@@ -12,9 +12,11 @@ from twisted.internet.error import ConnectionClosed from twisted.python.failure import Failure from twisted.web.client import ResponseFailed -from scrapy.exceptions import DownloadCancelledError+from scrapy import signals+from scrapy.exceptions import DownloadCancelledError, StopDownload from scrapy.http.headers import Headers from scrapy.utils._download_handlers import (+    check_stop_download,     get_maxsize_msg,     get_warnsize_msg,     make_response,@@ -25,6 +27,7 @@ if TYPE_CHECKING:     from collections.abc import Sequence      from scrapy.core.http2.protocol import H2ClientProtocol+    from scrapy.crawler import Crawler     from scrapy.http import Request, Response  @@ -82,6 +85,9 @@ class StreamCloseReason(Enum):     # Actual response body size is more than allowed limit     MAXSIZE_EXCEEDED_ACTUAL = 8 +    # A bytes_received or headers_received signal handler raised StopDownload+    STOP_DOWNLOAD = 9+  class Stream:     """Represents a single HTTP/2 Stream.@@ -101,16 +107,23 @@ class Stream:         protocol: H2ClientProtocol,         download_maxsize: int = 0,         download_warnsize: int = 0,+        crawler: Crawler | None = None,     ) -> None:         """         Arguments:             stream_id -- Unique identifier for the stream within a single HTTP/2 connection             request -- The HTTP request associated to the stream             protocol -- Parent H2ClientProtocol instance+            crawler -- Crawler used to send the bytes_received and+                headers_received signals, no signals are sent if None         """         self.stream_id: int = stream_id         self._request: Request = request         self._protocol: H2ClientProtocol = protocol+        self._crawler: Crawler | None = crawler++        # Set when a signal handler raises StopDownload+        self._stop_download: StopDownload | None = None          self._download_maxsize = self._request.meta.get(             "download_maxsize", download_maxsize@@ -338,6 +351,23 @@ class Stream:         self._response["body"].write(data)         self._response["flow_controlled_size"] += flow_controlled_length +        if (+            data+            and self._crawler is not None+            and (+                stop_download := check_stop_download(+                    signals.bytes_received, self._crawler, self._request, data=data+                )+            )+        ):+            # Give the connection flow control window back, as the data of+            # this frame will not be acknowledged otherwise+            self._protocol.conn.acknowledge_received_data(+                flow_controlled_length, self.stream_id+            )+            self.stop_download(stop_download)+            return+         # We check maxsize here in case the Content-Length header was not received         if (             self._download_maxsize@@ -369,8 +399,21 @@ class Stream:             else:                 self._response["headers"].appendlist(name, value) -        # Check if we exceed the allowed max data size which can be received         expected_size = int(self._response["headers"].get(b"Content-Length", -1))++        if self._crawler is not None and (+            stop_download := check_stop_download(+                signals.headers_received,+                self._crawler,+                self._request,+                headers=self._response["headers"],+                body_length=expected_size,+            )+        ):+            self.stop_download(stop_download)+            return++        # Check if we exceed the allowed max data size which can be received         if self._download_maxsize and expected_size > self._download_maxsize:             self.reset_stream(StreamCloseReason.MAXSIZE_EXCEEDED)             return@@ -382,6 +425,27 @@ class Stream:             )             logger.warning(warning_msg) +    def stop_download(self, stop_download: StopDownload) -> None:+        """Stop receiving the response as requested by a signal handler and+        fire the response deferred with the data received so far"""+        if self.metadata["stream_closed_local"]:+            raise StreamClosedError(self.stream_id)++        self._stop_download = stop_download+        self.metadata["stream_closed_local"] = True+        try:+            self._protocol.conn.reset_stream(self.stream_id, ErrorCodes.CANCEL)+        except StreamClosedError:+            # The frame being processed carried END_STREAM, so the remote has+            # already closed the stream and there is nothing left to reset.+            logger.debug(+                "Stream %s of %r was already closed by the remote when "+                "stopping the download",+                self.stream_id,+                self._request,+            )+        self.close(StreamCloseReason.STOP_DOWNLOAD)+     def reset_stream(self, reason: StreamCloseReason = StreamCloseReason.RESET) -> None:         """Close this stream by sending a RST_FRAME to the remote peer"""         if self.metadata["stream_closed_local"]:@@ -447,6 +511,9 @@ class Stream:         elif reason is StreamCloseReason.ENDED:             self._fire_response_deferred() +        elif reason is StreamCloseReason.STOP_DOWNLOAD:+            self._fire_response_deferred(stop_download=self._stop_download)+         # Stream was abruptly ended here         elif reason is StreamCloseReason.CANCELLED:             # Client has cancelled the request. Remove all the data@@ -490,18 +557,28 @@ class Stream:                 )             ) -    def _fire_response_deferred(self) -> None:+    def _fire_response_deferred(+        self, stop_download: StopDownload | None = None+    ) -> None:         """Builds response from the self._response dict         and fires the response deferred callback with the-        generated response instance"""--        response = make_response(-            url=self._request.url,-            status=self._response["status"],-            headers=self._response["headers"],-            body=self._response["body"].getvalue(),-            certificate=self._protocol.metadata["certificate"],-            ip_address=self._protocol.metadata["ip_address"],-            protocol="h2",-        )+        generated response instance++        If stop_download has fail=True, the response deferred errback is+        fired with it instead, with the response set as its attribute"""++        try:+            response = make_response(+                url=self._request.url,+                status=self._response["status"],+                headers=self._response["headers"],+                body=self._response["body"].getvalue(),+                certificate=self._protocol.metadata["certificate"],+                ip_address=self._protocol.metadata["ip_address"],+                protocol="h2",+                stop_download=stop_download,+            )+        except StopDownload as e:+            self._deferred_response.errback(e)+            return         self._deferred_response.callback(response)tests/test_downloader_handler_twisted_http2.pyindex 449f2d635..9a9cfe159 100644@@ -9,10 +9,11 @@ from typing import TYPE_CHECKING, Any import pytest from twisted.web.http import H2_ENABLED -from scrapy import Spider+from scrapy import Spider, signals+from scrapy.core.downloader.handlers.base import BaseDownloadHandler from scrapy.crawler import Crawler-from scrapy.exceptions import DownloadFailedError, NotConfigured-from scrapy.http import Request+from scrapy.exceptions import DownloadFailedError, NotConfigured, StopDownload+from scrapy.http import Headers, Request from tests.utils.bases.download_handlers_http import (     TestHttpProxyBase,     TestHttpsBase,@@ -61,6 +62,11 @@ class H2DownloadHandlerMixin:         }  +def _crawler(download_handler: DownloadHandlerProtocol) -> Crawler:+    assert isinstance(download_handler, BaseDownloadHandler)+    return download_handler.crawler++ def test_not_configured_without_reactor() -> None:     from scrapy.core.downloader.handlers.http2 import H2DownloadHandler  # noqa: PLC0415 @@ -156,6 +162,82 @@ class TestHttp2(H2DownloadHandlerMixin, TestHttpsBase):             with pytest.raises(DownloadFailedError):                 await download_handler.download_request(request) +    @coroutine_test+    async def test_headers_received_stop_download_end_stream(+        self, mockserver: MockServer+    ) -> None:+        # the HEADERS frame of a response without a body ends the stream+        received: list[tuple[Headers, int]] = []++        def headers_received(+            headers: Headers, body_length: int, request: Request, spider: Spider+        ) -> None:+            received.append((headers, body_length))+            raise StopDownload(fail=False)++        request = Request(mockserver.url("/status?n=204", is_secure=self.is_secure))+        async with self.get_dh() as download_handler:+            _crawler(download_handler).signals.connect(+                headers_received, signals.headers_received+            )+            response = await download_handler.download_request(request)+        assert len(received) == 1+        assert response.status == 204+        assert response.headers == received[0][0]+        assert "download_stopped" in response.flags++    @coroutine_test+    async def test_bytes_received_stop_download_end_stream(+        self, mockserver: MockServer+    ) -> None:+        received: list[bytes] = []++        def bytes_received(data: bytes, request: Request, spider: Spider) -> None:+            received.append(data)+            raise StopDownload(fail=True)++        request = Request(mockserver.url("/text", is_secure=self.is_secure))+        async with self.get_dh() as download_handler:+            _crawler(download_handler).signals.connect(+                bytes_received, signals.bytes_received+            )+            with pytest.raises(StopDownload) as exc_info:+                await download_handler.download_request(request)+        assert received == [b"Works"]+        assert exc_info.value.response is not None+        assert exc_info.value.response.body == b"Works"+        assert "download_stopped" in exc_info.value.response.flags++    @coroutine_test+    async def test_stop_download_keeps_connection_usable(+        self, mockserver: MockServer+    ) -> None:+        stop_urls: set[str] = set()++        def bytes_received(data: bytes, request: Request, spider: Spider) -> None:+            if request.url in stop_urls:+                raise StopDownload(fail=False)++        stopped_request = Request(+            mockserver.url("/alpayload", is_secure=self.is_secure),+            method="POST",+            body=b"a" * 2**18,+        )+        stop_urls.add(stopped_request.url)+        async with self.get_dh() as download_handler:+            _crawler(download_handler).signals.connect(+                bytes_received, signals.bytes_received+            )+            stopped = await download_handler.download_request(stopped_request)+            # the next request reuses the same HTTP/2 connection+            response = await download_handler.download_request(+                Request(mockserver.url("/text", is_secure=self.is_secure))+            )+        assert "download_stopped" in stopped.flags+        assert len(stopped.body) < 2**18+        assert response.body == b"Works"+        assert "download_stopped" not in response.flags+  class TestSimpleHttp2(H2DownloadHandlerMixin, TestSimpleHttpsBase):     pass@@ -186,18 +268,6 @@ class TestHttp2TLSVersion(H2DownloadHandlerMixin, TestHttpsTLSVersionBase): class TestHttp2WithCrawler(H2DownloadHandlerMixin, TestHttpWithCrawlerBase):     is_secure = True -    def test_bytes_received_stop_download_callback(self) -> None:  # type: ignore[override]-        pytest.skip("bytes_received support is not implemented")--    def test_bytes_received_stop_download_errback(self) -> None:  # type: ignore[override]-        pytest.skip("bytes_received support is not implemented")--    def test_headers_received_stop_download_callback(self) -> None:  # type: ignore[override]-        pytest.skip("headers_received support is not implemented")--    def test_headers_received_stop_download_errback(self) -> None:  # type: ignore[override]-        pytest.skip("headers_received support is not implemented")-  @pytest.mark.skip(reason="Proxy support is not implemented yet") class TestHttp2Proxy(H2DownloadHandlerMixin, TestHttpProxyBase):tests/test_http2_client_protocol.pyindex b8586d1ca..a99528356 100644@@ -765,3 +765,15 @@ class TestHttps2ClientProtocol:             k_decoded, v_decoded = str(k, "utf-8"), str(v[0], "utf-8")             assert k_decoded in response_headers             assert v_decoded == response_headers[k_decoded]+++def test_window_updated_for_closed_stream() -> None:+    from scrapy.core.http2.protocol import H2ClientProtocol  # noqa: PLC0415++    uri = URI.fromBytes(b"https://example.com/")+    protocol = H2ClientProtocol(uri, Settings(), Deferred())+    event = mock.Mock(stream_id=1, delta=100)+    # a stream closed locally (e.g. a stopped download) is no longer tracked,+    # late WINDOW_UPDATE frames for it must be ignored+    protocol.window_updated(event)+    assert protocol.streams == {} 
Merged fix · PR #7896+76 −47
docs/topics/download-handlers.rstindex e0501c169..94e75ab6f 100644@@ -203,9 +203,6 @@ Other limitations: -   IPv6 support requires setting :setting:`TWISTED_DNS_RESOLVER`     to ``scrapy.resolver.CachingHostnameResolver``. --   No support for the :signal:`bytes_received` and :signal:`headers_received`-    signals.- Known limitations of the HTTP/2 support:  -   No support for HTTP/2 Cleartext (h2c), since no major browser supportsscrapy/core/downloader/handlers/http2.pyindex f60c58d1b..9b3d4fbd4 100644@@ -40,7 +40,7 @@ class H2DownloadHandler(BaseDownloadHandler):          from twisted.internet import reactor -        self._pool = H2ConnectionPool(reactor, crawler.settings)+        self._pool = H2ConnectionPool(reactor, crawler)         self._context_factory = _load_context_factory_from_settings(crawler)         self._bind_address = crawler.settings.get("DOWNLOAD_BIND_ADDRESS") scrapy/core/http2/agent.pyindex aa55e29a0..042557208 100644@@ -21,8 +21,8 @@ if TYPE_CHECKING:     from twisted.internet.base import ReactorBase     from twisted.internet.endpoints import HostnameEndpoint +    from scrapy.crawler import Crawler     from scrapy.http import Request, Response-    from scrapy.settings import Settings     from scrapy.spiders import Spider  @@ -30,9 +30,9 @@ ConnectionKeyT = tuple[bytes, bytes, int]   class H2ConnectionPool:-    def __init__(self, reactor: ReactorBase, settings: Settings) -> None:+    def __init__(self, reactor: ReactorBase, crawler: Crawler) -> None:         self._reactor = reactor-        self.settings = settings+        self._crawler = crawler          # Store a dictionary which is used to get the respective         # H2ClientProtocolInstance using the  key as Tuple(scheme, hostname, port)@@ -43,7 +43,7 @@ class H2ConnectionPool:             ConnectionKeyT, deque[Deferred[H2ClientProtocol]]         ] = {} -        self._tls_verbose_logging: bool = settings.getbool(+        self._tls_verbose_logging: bool = crawler.settings.getbool(             "DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING"         ) @@ -77,7 +77,7 @@ class H2ConnectionPool:          factory = H2ClientFactory(             uri,-            self.settings,+            self._crawler,             conn_lost_deferred,             tls_verbose_logging=self._tls_verbose_logging,         )scrapy/core/http2/protocol.pyindex 7136e829e..2d59aba31 100644@@ -44,7 +44,7 @@ if TYPE_CHECKING:     from twisted.python.failure import Failure     from twisted.web.client import URI -    from scrapy.settings import Settings+    from scrapy.crawler import Crawler     from scrapy.spiders import Spider  @@ -90,7 +90,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):     def __init__(         self,         uri: URI,-        settings: Settings,+        crawler: Crawler,         conn_lost_deferred: Deferred[list[BaseException]],         *,         tls_verbose_logging: bool = False,@@ -100,11 +100,12 @@ class H2ClientProtocol(Protocol, TimeoutMixin):             uri -- URI of the base url to which HTTP/2 Connection will be made.                 uri is used to verify that incoming client requests have correct                 base URL.-            settings -- Scrapy project settings+            crawler -- The crawler the requests belong to             conn_lost_deferred -- Deferred that fires with the list of underlying exceptions to notify                 that connection was lost             tls_verbose_logging -- Whether to log TLS details         """+        self._crawler: Crawler = crawler         self._conn_lost_deferred: Deferred[list[BaseException]] = conn_lost_deferred         self._tls_verbose_logging: bool = tls_verbose_logging @@ -140,8 +141,8 @@ class H2ClientProtocol(Protocol, TimeoutMixin):             # Both ip_address and uri are used by the Stream before             # initiating the request to verify that the base address             # Variables taken from Project Settings-            "default_download_maxsize": settings.getint("DOWNLOAD_MAXSIZE"),-            "default_download_warnsize": settings.getint("DOWNLOAD_WARNSIZE"),+            "default_download_maxsize": crawler.settings.getint("DOWNLOAD_MAXSIZE"),+            "default_download_warnsize": crawler.settings.getint("DOWNLOAD_WARNSIZE"),             # Counter to keep track of opened streams. This counter             # is used to make sure that not more than MAX_CONCURRENT_STREAMS             # streams are opened which leads to ProtocolError@@ -208,6 +209,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):             stream_id=next(self._stream_id_generator),             request=request,             protocol=self,+            crawler=self._crawler,             download_maxsize=getattr(                 spider, "download_maxsize", self.metadata["default_download_maxsize"]             ),@@ -461,20 +463,20 @@ class H2ClientFactory(Factory):     def __init__(         self,         uri: URI,-        settings: Settings,+        crawler: Crawler,         conn_lost_deferred: Deferred[list[BaseException]],         *,         tls_verbose_logging: bool = False,     ) -> None:         self.uri = uri-        self.settings = settings+        self.crawler = crawler         self.conn_lost_deferred = conn_lost_deferred         self.tls_verbose_logging = tls_verbose_logging      def buildProtocol(self, addr: IAddress) -> H2ClientProtocol:         return H2ClientProtocol(             self.uri,-            self.settings,+            self.crawler,             self.conn_lost_deferred,             tls_verbose_logging=self.tls_verbose_logging,         )scrapy/core/http2/stream.pyindex c6226bbca..4fc300d90 100644@@ -1,6 +1,7 @@ from __future__ import annotations  import logging+from contextlib import suppress from enum import Enum from io import BytesIO from typing import TYPE_CHECKING, Any@@ -12,9 +13,11 @@ from twisted.internet.error import ConnectionClosed from twisted.python.failure import Failure from twisted.web.client import ResponseFailed -from scrapy.exceptions import DownloadCancelledError+from scrapy import signals+from scrapy.exceptions import DownloadCancelledError, StopDownload from scrapy.http.headers import Headers from scrapy.utils._download_handlers import (+    check_stop_download,     get_maxsize_msg,     get_warnsize_msg,     make_response,@@ -25,6 +28,7 @@ if TYPE_CHECKING:     from collections.abc import Sequence      from scrapy.core.http2.protocol import H2ClientProtocol+    from scrapy.crawler import Crawler     from scrapy.http import Request, Response  @@ -82,6 +86,9 @@ class StreamCloseReason(Enum):     # Actual response body size is more than allowed limit     MAXSIZE_EXCEEDED_ACTUAL = 8 +    # A signal handler raised StopDownload+    STOP_DOWNLOAD = 9+  class Stream:     """Represents a single HTTP/2 Stream.@@ -99,6 +106,7 @@ class Stream:         stream_id: int,         request: Request,         protocol: H2ClientProtocol,+        crawler: Crawler,         download_maxsize: int = 0,         download_warnsize: int = 0,     ) -> None:@@ -107,10 +115,13 @@ class Stream:             stream_id -- Unique identifier for the stream within a single HTTP/2 connection             request -- The HTTP request associated to the stream             protocol -- Parent H2ClientProtocol instance+            crawler -- The crawler the request belongs to         """         self.stream_id: int = stream_id         self._request: Request = request         self._protocol: H2ClientProtocol = protocol+        self._crawler: Crawler = crawler+        self._stop_download: StopDownload | None = None          self._download_maxsize = self._request.meta.get(             "download_maxsize", download_maxsize@@ -338,6 +349,13 @@ class Stream:         self._response["body"].write(data)         self._response["flow_controlled_size"] += flow_controlled_length +        if stop_download := check_stop_download(+            signals.bytes_received, self._crawler, self._request, data=data+        ):+            self._stop_download = stop_download+            self.reset_stream(StreamCloseReason.STOP_DOWNLOAD)+            return+         # We check maxsize here in case the Content-Length header was not received         if (             self._download_maxsize@@ -369,8 +387,20 @@ class Stream:             else:                 self._response["headers"].appendlist(name, value) -        # Check if we exceed the allowed max data size which can be received         expected_size = int(self._response["headers"].get(b"Content-Length", -1))++        if stop_download := check_stop_download(+            signals.headers_received,+            self._crawler,+            self._request,+            headers=self._response["headers"],+            body_length=expected_size if expected_size >= 0 else None,+        ):+            self._stop_download = stop_download+            self.reset_stream(StreamCloseReason.STOP_DOWNLOAD)+            return++        # Check if we exceed the allowed max data size which can be received         if self._download_maxsize and expected_size > self._download_maxsize:             self.reset_stream(StreamCloseReason.MAXSIZE_EXCEEDED)             return@@ -387,11 +417,18 @@ class Stream:         if self.metadata["stream_closed_local"]:             raise StreamClosedError(self.stream_id) -        # Clear buffer earlier to avoid keeping data in memory for a long time-        self._response["body"].truncate(0)+        # The data received so far is the body of the response built for a+        # stopped download, otherwise the buffer is cleared early to avoid+        # keeping data in memory for a long time+        if reason is not StreamCloseReason.STOP_DOWNLOAD:+            self._response["body"].truncate(0)          self.metadata["stream_closed_local"] = True-        self._protocol.conn.reset_stream(self.stream_id, ErrorCodes.REFUSED_STREAM)+        # The remote peer may have ended the stream already, e.g. because the+        # whole response arrived within the data that triggered this reset, in+        # which case there is nothing left to reset+        with suppress(StreamClosedError):+            self._protocol.conn.reset_stream(self.stream_id, ErrorCodes.REFUSED_STREAM)         self.close(reason)      def close(@@ -444,7 +481,7 @@ class Stream:             logger.error(error_msg)             self._deferred_response.errback(DownloadCancelledError(error_msg)) -        elif reason is StreamCloseReason.ENDED:+        elif reason in {StreamCloseReason.ENDED, StreamCloseReason.STOP_DOWNLOAD}:             self._fire_response_deferred()          # Stream was abruptly ended here@@ -495,13 +532,18 @@ class Stream:         and fires the response deferred callback with the         generated response instance""" -        response = make_response(-            url=self._request.url,-            status=self._response["status"],-            headers=self._response["headers"],-            body=self._response["body"].getvalue(),-            certificate=self._protocol.metadata["certificate"],-            ip_address=self._protocol.metadata["ip_address"],-            protocol="h2",-        )-        self._deferred_response.callback(response)+        try:+            response = make_response(+                url=self._request.url,+                status=self._response["status"],+                headers=self._response["headers"],+                body=self._response["body"].getvalue(),+                certificate=self._protocol.metadata["certificate"],+                ip_address=self._protocol.metadata["ip_address"],+                protocol="h2",+                stop_download=self._stop_download,+            )+        except StopDownload as exc:+            self._deferred_response.errback(exc)+        else:+            self._deferred_response.callback(response)tests/test_downloader_handler_twisted_http2.pyindex 449f2d635..9d4e161f3 100644@@ -186,18 +186,6 @@ class TestHttp2TLSVersion(H2DownloadHandlerMixin, TestHttpsTLSVersionBase): class TestHttp2WithCrawler(H2DownloadHandlerMixin, TestHttpWithCrawlerBase):     is_secure = True -    def test_bytes_received_stop_download_callback(self) -> None:  # type: ignore[override]-        pytest.skip("bytes_received support is not implemented")--    def test_bytes_received_stop_download_errback(self) -> None:  # type: ignore[override]-        pytest.skip("bytes_received support is not implemented")--    def test_headers_received_stop_download_callback(self) -> None:  # type: ignore[override]-        pytest.skip("headers_received support is not implemented")--    def test_headers_received_stop_download_errback(self) -> None:  # type: ignore[override]-        pytest.skip("headers_received support is not implemented")-  @pytest.mark.skip(reason="Proxy support is not implemented yet") class TestHttp2Proxy(H2DownloadHandlerMixin, TestHttpProxyBase):tests/test_http2_client_protocol.pyindex b8586d1ca..431b7a458 100644@@ -23,13 +23,13 @@ from twisted.web.static import File  from scrapy.exceptions import DownloadCancelledError, DownloadTimeoutError from scrapy.http import JsonRequest, Request, Response-from scrapy.settings import Settings from scrapy.spiders import Spider from scrapy.utils.defer import (     deferred_f_from_coro_f,     deferred_from_coro,     maybe_deferred_to_future, )+from scrapy.utils.test import get_crawler from tests.mockserver.http_resources import LeafResource, Status, put_child from tests.mockserver.utils import ssl_context_factory @@ -250,7 +250,7 @@ class TestHttps2ClientProtocol:             acceptableProtocols=[b"h2"],         )         uri = URI.fromBytes(bytes(self.get_url(server_port, "/"), "utf-8"))-        h2_client_factory = H2ClientFactory(uri, Settings(), Deferred())+        h2_client_factory = H2ClientFactory(uri, get_crawler(), Deferred())         client_endpoint = SSL4ClientEndpoint(             reactor, self.host, server_port, client_options         )