From 89a0b0857864e0a50eddfbf147cebe54d50bccc3 Mon Sep 17 00:00:00 2001 From: Priyankm23 Date: Wed, 30 Sep 2026 02:13:04 +0000 Subject: [PATCH 1/2] Fix thread-safety race in HTTP/2 HPACK header encoding and stream state (cherry picked from commit 7d48dccc40c5c6142241334b78b24b9b8d508876) --- httpcore/_async/http2.py | 85 ++++++++++++++++++++++++-------------- httpcore/_sync/http2.py | 85 ++++++++++++++++++++++++-------------- requirements.txt | 2 +- tests/_async/test_http2.py | 29 +++++++++++++ tests/_sync/test_http2.py | 29 +++++++++++++ 5 files changed, 169 insertions(+), 61 deletions(-) diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index dbd0beeb4..974679f5e 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -19,7 +19,12 @@ RemoteProtocolError, ) from .._models import Origin, Request, Response -from .._synchronization import AsyncLock, AsyncSemaphore, AsyncShieldCancellation +from .._synchronization import ( + AsyncLock, + AsyncSemaphore, + AsyncShieldCancellation, + AsyncThreadLock, +) from .._trace import Trace from .interfaces import AsyncConnectionInterface @@ -60,6 +65,11 @@ def __init__( self._state_lock = AsyncLock() self._read_lock = AsyncLock() self._write_lock = AsyncLock() + # Lock guarding in-memory h2 state transitions in multi-threaded sync mode. + # In async mode, AsyncThreadLock is a zero-cost no-op because asyncio is + # single-threaded; all operations inside this lock are strictly synchronous + # (no `await` expressions) and released before any blocking network I/O. + self._h2_thread_lock = AsyncThreadLock() self._sent_connection_init = False self._used_all_stream_ids = False self._connection_error = False @@ -130,18 +140,13 @@ async def handle_async_request(self, request: Request) -> Response: await self._max_streams_semaphore.acquire() + stream_id: int | None = None try: - stream_id = self._h2_state.get_next_available_stream_id() - self._events[stream_id] = [] - except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover - self._used_all_stream_ids = True - self._request_count -= 1 - raise ConnectionNotAvailable() - - try: - kwargs = {"request": request, "stream_id": stream_id} + kwargs: dict[str, typing.Any] = {"request": request} async with Trace("send_request_headers", logger, request, kwargs): - await self._send_request_headers(request=request, stream_id=stream_id) + stream_id = await self._send_request_headers(request=request) + kwargs["stream_id"] = stream_id + async with Trace("send_request_body", logger, request, kwargs): await self._send_request_body(request=request, stream_id=stream_id) async with Trace( @@ -163,10 +168,13 @@ async def handle_async_request(self, request: Request) -> Response: }, ) except BaseException as exc: # noqa: PIE786 - with AsyncShieldCancellation(): - kwargs = {"stream_id": stream_id} - async with Trace("response_closed", logger, request, kwargs): - await self._response_closed(stream_id=stream_id) + if stream_id is not None: + with AsyncShieldCancellation(): + response_closed_kwargs = {"stream_id": stream_id} + async with Trace( + "response_closed", logger, request, response_closed_kwargs + ): + await self._response_closed(stream_id=stream_id) if isinstance(exc, h2.exceptions.ProtocolError): # One case where h2 can raise a protocol error is when a @@ -219,7 +227,7 @@ async def _send_connection_init(self, request: Request) -> None: # Sending the request... - async def _send_request_headers(self, request: Request, stream_id: int) -> None: + async def _send_request_headers(self, request: Request) -> int: """ Send the request headers to a given stream ID. """ @@ -246,9 +254,20 @@ async def _send_request_headers(self, request: Request, stream_id: int) -> None: ) ] - self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) - self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + with self._h2_thread_lock: + try: + stream_id = self._h2_state.get_next_available_stream_id() + self._events[stream_id] = [] + except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover + self._used_all_stream_ids = True + self._request_count -= 1 + raise ConnectionNotAvailable() + + self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) + self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + await self._write_outgoing_data(request) + return stream_id async def _send_request_body(self, request: Request, stream_id: int) -> None: """ @@ -272,14 +291,16 @@ async def _send_stream_data( max_flow = await self._wait_for_outgoing_flow(request, stream_id) chunk_size = min(len(data), max_flow) chunk, data = data[:chunk_size], data[chunk_size:] - self._h2_state.send_data(stream_id, chunk) + with self._h2_thread_lock: + self._h2_state.send_data(stream_id, chunk) await self._write_outgoing_data(request) async def _send_end_stream(self, request: Request, stream_id: int) -> None: """ Send an empty data frame on on a given stream ID with the END_STREAM flag set. """ - self._h2_state.end_stream(stream_id) + with self._h2_thread_lock: + self._h2_state.end_stream(stream_id) await self._write_outgoing_data(request) # Receiving the response... @@ -318,7 +339,8 @@ async def _receive_response_body( assert event.flow_controlled_length is not None assert event.data is not None amount = event.flow_controlled_length - self._h2_state.acknowledge_received_data(amount, stream_id) + with self._h2_thread_lock: + self._h2_state.acknowledge_received_data(amount, stream_id) await self._write_outgoing_data(request) yield event.data elif isinstance(event, h2.events.StreamEnded): @@ -422,9 +444,8 @@ async def _response_closed(self, stream_id: int) -> None: await self.aclose() async def aclose(self) -> None: - # Note that this method unilaterally closes the connection, and does - # not have any kind of locking in place around it. - self._h2_state.close_connection() + with self._h2_thread_lock: + self._h2_state.close_connection() self._state = HTTPConnectionState.CLOSED await self._network_stream.aclose() @@ -454,7 +475,8 @@ async def _read_incoming_data(self, request: Request) -> list[h2.events.Event]: self._connection_error = True raise exc - events: list[h2.events.Event] = self._h2_state.receive_data(data) + with self._h2_thread_lock: + events: list[h2.events.Event] = self._h2_state.receive_data(data) return events @@ -463,7 +485,8 @@ async def _write_outgoing_data(self, request: Request) -> None: timeout = timeouts.get("write", None) async with self._write_lock: - data_to_send = self._h2_state.data_to_send() + with self._h2_thread_lock: + data_to_send = self._h2_state.data_to_send() if self._write_exception is not None: raise self._write_exception # pragma: nocover @@ -493,13 +516,15 @@ async def _wait_for_outgoing_flow(self, request: Request, stream_id: int) -> int WindowUpdated frames have increased the flow rate. https://tools.ietf.org/html/rfc7540#section-6.9 """ - local_flow: int = self._h2_state.local_flow_control_window(stream_id) - max_frame_size: int = self._h2_state.max_outbound_frame_size + with self._h2_thread_lock: + local_flow: int = self._h2_state.local_flow_control_window(stream_id) + max_frame_size: int = self._h2_state.max_outbound_frame_size flow = min(local_flow, max_frame_size) while flow == 0: await self._receive_events(request) - local_flow = self._h2_state.local_flow_control_window(stream_id) - max_frame_size = self._h2_state.max_outbound_frame_size + with self._h2_thread_lock: + local_flow = self._h2_state.local_flow_control_window(stream_id) + max_frame_size = self._h2_state.max_outbound_frame_size flow = min(local_flow, max_frame_size) return flow diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index ddcc18900..4daf8eadb 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -19,7 +19,12 @@ RemoteProtocolError, ) from .._models import Origin, Request, Response -from .._synchronization import Lock, Semaphore, ShieldCancellation +from .._synchronization import ( + Lock, + Semaphore, + ShieldCancellation, + ThreadLock, +) from .._trace import Trace from .interfaces import ConnectionInterface @@ -60,6 +65,11 @@ def __init__( self._state_lock = Lock() self._read_lock = Lock() self._write_lock = Lock() + # Lock guarding in-memory h2 state transitions in multi-threaded sync mode. + # In async mode, ThreadLock is a zero-cost no-op because asyncio is + # single-threaded; all operations inside this lock are strictly synchronous + # (no `await` expressions) and released before any blocking network I/O. + self._h2_thread_lock = ThreadLock() self._sent_connection_init = False self._used_all_stream_ids = False self._connection_error = False @@ -130,18 +140,13 @@ def handle_request(self, request: Request) -> Response: self._max_streams_semaphore.acquire() + stream_id: int | None = None try: - stream_id = self._h2_state.get_next_available_stream_id() - self._events[stream_id] = [] - except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover - self._used_all_stream_ids = True - self._request_count -= 1 - raise ConnectionNotAvailable() - - try: - kwargs = {"request": request, "stream_id": stream_id} + kwargs: dict[str, typing.Any] = {"request": request} with Trace("send_request_headers", logger, request, kwargs): - self._send_request_headers(request=request, stream_id=stream_id) + stream_id = self._send_request_headers(request=request) + kwargs["stream_id"] = stream_id + with Trace("send_request_body", logger, request, kwargs): self._send_request_body(request=request, stream_id=stream_id) with Trace( @@ -163,10 +168,13 @@ def handle_request(self, request: Request) -> Response: }, ) except BaseException as exc: # noqa: PIE786 - with ShieldCancellation(): - kwargs = {"stream_id": stream_id} - with Trace("response_closed", logger, request, kwargs): - self._response_closed(stream_id=stream_id) + if stream_id is not None: + with ShieldCancellation(): + response_closed_kwargs = {"stream_id": stream_id} + with Trace( + "response_closed", logger, request, response_closed_kwargs + ): + self._response_closed(stream_id=stream_id) if isinstance(exc, h2.exceptions.ProtocolError): # One case where h2 can raise a protocol error is when a @@ -219,7 +227,7 @@ def _send_connection_init(self, request: Request) -> None: # Sending the request... - def _send_request_headers(self, request: Request, stream_id: int) -> None: + def _send_request_headers(self, request: Request) -> int: """ Send the request headers to a given stream ID. """ @@ -246,9 +254,20 @@ def _send_request_headers(self, request: Request, stream_id: int) -> None: ) ] - self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) - self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + with self._h2_thread_lock: + try: + stream_id = self._h2_state.get_next_available_stream_id() + self._events[stream_id] = [] + except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover + self._used_all_stream_ids = True + self._request_count -= 1 + raise ConnectionNotAvailable() + + self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) + self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + self._write_outgoing_data(request) + return stream_id def _send_request_body(self, request: Request, stream_id: int) -> None: """ @@ -272,14 +291,16 @@ def _send_stream_data( max_flow = self._wait_for_outgoing_flow(request, stream_id) chunk_size = min(len(data), max_flow) chunk, data = data[:chunk_size], data[chunk_size:] - self._h2_state.send_data(stream_id, chunk) + with self._h2_thread_lock: + self._h2_state.send_data(stream_id, chunk) self._write_outgoing_data(request) def _send_end_stream(self, request: Request, stream_id: int) -> None: """ Send an empty data frame on on a given stream ID with the END_STREAM flag set. """ - self._h2_state.end_stream(stream_id) + with self._h2_thread_lock: + self._h2_state.end_stream(stream_id) self._write_outgoing_data(request) # Receiving the response... @@ -318,7 +339,8 @@ def _receive_response_body( assert event.flow_controlled_length is not None assert event.data is not None amount = event.flow_controlled_length - self._h2_state.acknowledge_received_data(amount, stream_id) + with self._h2_thread_lock: + self._h2_state.acknowledge_received_data(amount, stream_id) self._write_outgoing_data(request) yield event.data elif isinstance(event, h2.events.StreamEnded): @@ -422,9 +444,8 @@ def _response_closed(self, stream_id: int) -> None: self.close() def close(self) -> None: - # Note that this method unilaterally closes the connection, and does - # not have any kind of locking in place around it. - self._h2_state.close_connection() + with self._h2_thread_lock: + self._h2_state.close_connection() self._state = HTTPConnectionState.CLOSED self._network_stream.close() @@ -454,7 +475,8 @@ def _read_incoming_data(self, request: Request) -> list[h2.events.Event]: self._connection_error = True raise exc - events: list[h2.events.Event] = self._h2_state.receive_data(data) + with self._h2_thread_lock: + events: list[h2.events.Event] = self._h2_state.receive_data(data) return events @@ -463,7 +485,8 @@ def _write_outgoing_data(self, request: Request) -> None: timeout = timeouts.get("write", None) with self._write_lock: - data_to_send = self._h2_state.data_to_send() + with self._h2_thread_lock: + data_to_send = self._h2_state.data_to_send() if self._write_exception is not None: raise self._write_exception # pragma: nocover @@ -493,13 +516,15 @@ def _wait_for_outgoing_flow(self, request: Request, stream_id: int) -> int: WindowUpdated frames have increased the flow rate. https://tools.ietf.org/html/rfc7540#section-6.9 """ - local_flow: int = self._h2_state.local_flow_control_window(stream_id) - max_frame_size: int = self._h2_state.max_outbound_frame_size + with self._h2_thread_lock: + local_flow: int = self._h2_state.local_flow_control_window(stream_id) + max_frame_size: int = self._h2_state.max_outbound_frame_size flow = min(local_flow, max_frame_size) while flow == 0: self._receive_events(request) - local_flow = self._h2_state.local_flow_control_window(stream_id) - max_frame_size = self._h2_state.max_outbound_frame_size + with self._h2_thread_lock: + local_flow = self._h2_state.local_flow_control_window(stream_id) + max_frame_size = self._h2_state.max_outbound_frame_size flow = min(local_flow, max_frame_size) return flow diff --git a/requirements.txt b/requirements.txt index 90219a8ce..397452f08 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ jinja2==3.1.6 # Packaging build==1.2.2.post1 -twine==6.1.0 +twine==7.0.0 # Tests & Linting coverage[toml]==7.5.4 diff --git a/tests/_async/test_http2.py b/tests/_async/test_http2.py index b4ec66488..a9c9d5a4b 100644 --- a/tests/_async/test_http2.py +++ b/tests/_async/test_http2.py @@ -1,6 +1,7 @@ import hpack import hyperframe.frame import pytest +import trio as concurrency import httpcore @@ -380,3 +381,31 @@ async def test_http2_remote_max_streams_update(): conn._h2_state.local_settings.max_concurrent_streams, ) i += 1 + + +@pytest.mark.trio +async def test_http2_concurrent_send_request_headers() -> None: + origin = httpcore.Origin(b"https", b"example.com", 443) + stream = httpcore.AsyncMockStream([]) + conn = httpcore.AsyncHTTP2Connection(origin=origin, stream=stream) + conn._sent_connection_init = True + + async def send_headers(worker_id: int) -> None: + request = httpcore.Request( + method=b"GET", + url=f"https://example.com/item/{worker_id}", + headers=[ + (b"host", b"example.com"), + (b"user-agent", b"httpcore-test"), + ] + + [ + (f"x-custom-{k}".encode(), f"val-{k}-{worker_id}".encode()) + for k in range(15) + ], + ) + stream_id = await conn._send_request_headers(request=request) + assert stream_id > 0 + + async with concurrency.open_nursery() as nursery: + for i in range(10): + nursery.start_soon(send_headers, i) diff --git a/tests/_sync/test_http2.py b/tests/_sync/test_http2.py index 695359bd6..1cdf8d2d4 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -1,6 +1,7 @@ import hpack import hyperframe.frame import pytest +from tests import concurrency import httpcore @@ -380,3 +381,31 @@ def test_http2_remote_max_streams_update(): conn._h2_state.local_settings.max_concurrent_streams, ) i += 1 + + + +def test_http2_concurrent_send_request_headers() -> None: + origin = httpcore.Origin(b"https", b"example.com", 443) + stream = httpcore.MockStream([]) + conn = httpcore.HTTP2Connection(origin=origin, stream=stream) + conn._sent_connection_init = True + + def send_headers(worker_id: int) -> None: + request = httpcore.Request( + method=b"GET", + url=f"https://example.com/item/{worker_id}", + headers=[ + (b"host", b"example.com"), + (b"user-agent", b"httpcore-test"), + ] + + [ + (f"x-custom-{k}".encode(), f"val-{k}-{worker_id}".encode()) + for k in range(15) + ], + ) + stream_id = conn._send_request_headers(request=request) + assert stream_id > 0 + + with concurrency.open_nursery() as nursery: + for i in range(10): + nursery.start_soon(send_headers, i) From 43c8d1a6c91350348e5b46ac66ef16d342826c26 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 06:42:11 +0000 Subject: [PATCH 2/2] Restore safe cleanup when HTTP/2 header send is interrupted Place the stream ID in the caller before awaiting socket writes, so cancellation releases active-stream capacity and removes allocated events. Clean up before-allocation errors, and preserve Python 3.8-compatible development requirements. Add deterministic concurrent stream-ID and cancellation regressions. --- httpcore/_async/http2.py | 23 ++-- httpcore/_sync/http2.py | 23 ++-- requirements.txt | 2 +- tests/_sync/test_http2_thread_safety.py | 160 ++++++++++++++++++++++++ tests/test_http2_send_cancellation.py | 87 +++++++++++++ 5 files changed, 280 insertions(+), 15 deletions(-) create mode 100644 tests/_sync/test_http2_thread_safety.py create mode 100644 tests/test_http2_send_cancellation.py diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index 974679f5e..1a94e303f 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -145,7 +145,8 @@ async def handle_async_request(self, request: Request) -> Response: kwargs: dict[str, typing.Any] = {"request": request} async with Trace("send_request_headers", logger, request, kwargs): stream_id = await self._send_request_headers(request=request) - kwargs["stream_id"] = stream_id + kwargs["stream_id"] = stream_id + await self._write_outgoing_data(request) async with Trace("send_request_body", logger, request, kwargs): await self._send_request_body(request=request, stream_id=stream_id) @@ -168,8 +169,12 @@ async def handle_async_request(self, request: Request) -> Response: }, ) except BaseException as exc: # noqa: PIE786 - if stream_id is not None: - with AsyncShieldCancellation(): + with AsyncShieldCancellation(): + if stream_id is None: + # An error before stream allocation must release the permit. + await self._max_streams_semaphore.release() + self._request_count -= 1 + else: response_closed_kwargs = {"stream_id": stream_id} async with Trace( "response_closed", logger, request, response_closed_kwargs @@ -260,13 +265,17 @@ async def _send_request_headers(self, request: Request) -> int: self._events[stream_id] = [] except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover self._used_all_stream_ids = True - self._request_count -= 1 raise ConnectionNotAvailable() - self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) - self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + try: + self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) + self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + except BaseException: + # The caller has not received stream_id yet; do not retain + # an orphaned event entry when a header fails to encode. + self._events.pop(stream_id, None) + raise - await self._write_outgoing_data(request) return stream_id async def _send_request_body(self, request: Request, stream_id: int) -> None: diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index 4daf8eadb..417b3e15a 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -145,7 +145,8 @@ def handle_request(self, request: Request) -> Response: kwargs: dict[str, typing.Any] = {"request": request} with Trace("send_request_headers", logger, request, kwargs): stream_id = self._send_request_headers(request=request) - kwargs["stream_id"] = stream_id + kwargs["stream_id"] = stream_id + self._write_outgoing_data(request) with Trace("send_request_body", logger, request, kwargs): self._send_request_body(request=request, stream_id=stream_id) @@ -168,8 +169,12 @@ def handle_request(self, request: Request) -> Response: }, ) except BaseException as exc: # noqa: PIE786 - if stream_id is not None: - with ShieldCancellation(): + with ShieldCancellation(): + if stream_id is None: + # An error before stream allocation must release the permit. + self._max_streams_semaphore.release() + self._request_count -= 1 + else: response_closed_kwargs = {"stream_id": stream_id} with Trace( "response_closed", logger, request, response_closed_kwargs @@ -260,13 +265,17 @@ def _send_request_headers(self, request: Request) -> int: self._events[stream_id] = [] except h2.exceptions.NoAvailableStreamIDError: # pragma: nocover self._used_all_stream_ids = True - self._request_count -= 1 raise ConnectionNotAvailable() - self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) - self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + try: + self._h2_state.send_headers(stream_id, headers, end_stream=end_stream) + self._h2_state.increment_flow_control_window(2**24, stream_id=stream_id) + except BaseException: + # The caller has not received stream_id yet; do not retain + # an orphaned event entry when a header fails to encode. + self._events.pop(stream_id, None) + raise - self._write_outgoing_data(request) return stream_id def _send_request_body(self, request: Request, stream_id: int) -> None: diff --git a/requirements.txt b/requirements.txt index 397452f08..90219a8ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ jinja2==3.1.6 # Packaging build==1.2.2.post1 -twine==7.0.0 +twine==6.1.0 # Tests & Linting coverage[toml]==7.5.4 diff --git a/tests/_sync/test_http2_thread_safety.py b/tests/_sync/test_http2_thread_safety.py new file mode 100644 index 000000000..d37cd445a --- /dev/null +++ b/tests/_sync/test_http2_thread_safety.py @@ -0,0 +1,160 @@ +"""Regression test for encode/httpx#3566. + +`httpx.Client(http2=True)` shares a single connection across threads (HTTP/2 +multiplexing). That connection is `httpcore.HTTP2Connection`, which wraps one +`h2.H2Connection` state machine. The send path — stream-ID allocation, the +`_events` mapping, and the HPACK encoding in `send_headers` — used to be +completely unserialized, so concurrent threads could corrupt the state +machine ("deque mutated during iteration", "dictionary changed size during +iteration", `StreamIDTooLowError`). + +This test drives one shared `HTTP2Connection` from multiple threads over a +fully in-memory fake HTTP/2 server (a real server-side `h2` connection +guarded by its own lock, so only the *client-side* httpcore code is under +test) and asserts every request succeeds. +""" + +import threading +import time +import typing + +import h2.config +import h2.connection +import h2.events + +import httpcore + + +class FakeStream(httpcore.NetworkStream): + """In-memory full-duplex socket backed by a real server-side h2 connection.""" + + def __init__(self) -> None: + self._server = h2.connection.H2Connection( + config=h2.config.H2Configuration(client_side=False) + ) + self._server.initiate_connection() + self._lock = threading.Lock() + self._cond = threading.Condition(self._lock) + self._closed = False + self._responded: typing.Set[int] = set() + + def _respond(self, stream_id: int) -> None: + self._server.send_headers( + stream_id, + [(":status", "200"), ("content-length", "2")], + end_stream=False, + ) + self._server.send_data(stream_id, b"ok", end_stream=True) + + # -- NetworkStream interface -- + def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + with self._lock: + if buffer: + for event in self._server.receive_data(buffer): + if isinstance(event, h2.events.RequestReceived): + if event.stream_ended is not None: + self._responded.add(event.stream_id) + self._respond(event.stream_id) + elif isinstance(event, h2.events.DataReceived): + # Replenish the server's inbound flow-control window, + # like any real HTTP/2 server does. + self._server.acknowledge_received_data( + event.flow_controlled_length, event.stream_id + ) + elif isinstance(event, h2.events.StreamEnded): + if event.stream_id not in self._responded: + self._responded.add(event.stream_id) + self._respond(event.stream_id) + self._cond.notify_all() + + def read(self, max_bytes: int, timeout: typing.Optional[float] = None) -> bytes: + deadline = None if timeout is None else time.monotonic() + timeout + with self._lock: + while True: + data: bytes = self._server.data_to_send(max_bytes) + if data: + return data + if self._closed: # pragma: no cover + return b"" + remaining = ( + None if deadline is None else deadline - time.monotonic() + ) # pragma: no cover + if remaining is not None and remaining <= 0: # pragma: no cover + return b"" + self._cond.wait(timeout=1.0) # pragma: no cover + + def close(self) -> None: # pragma: no cover + with self._lock: + self._closed = True + self._cond.notify_all() + + def get_extra_info(self, info: str) -> typing.Any: # pragma: no cover + return None + + +def test_http2_connection_is_thread_safe(): + """ + One shared HTTP2Connection must survive N threads racing the send path. + + Without the `_send_lock` fix, this fails with errors such as + "deque mutated during iteration", "dictionary changed size during + iteration", `StreamIDTooLowError`, or `KeyError`. + """ + n_threads = 8 + n_requests = 50 + + origin = httpcore.Origin(b"https", b"example.org", 443) + connection = httpcore.HTTP2Connection(origin=origin, stream=FakeStream()) + original_get_id = connection._h2_state.get_next_available_stream_id + + def racing_get_id(): + next_id = original_get_id() + time.sleep(0.002) + return next_id + + connection._h2_state.get_next_available_stream_id = racing_get_id + + errors = [] + errors_lock = threading.Lock() + + def worker(worker_id): + for i in range(n_requests): + body = b"x" * 64 if i % 2 else b"" + headers = [ + (b"host", b"example.org"), + (b"user-agent", b"thread-safety-test"), + (b"x-request", f"{worker_id}-{i}".encode()), + ] + if body: + headers.append((b"content-length", str(len(body)).encode())) + request = httpcore.Request( + "POST" if body else "GET", + f"https://example.org/{worker_id}/{i}", + headers=headers, + content=body, + extensions={"timeout": {"read": 10, "write": 10}}, + ) + try: + response = connection.handle_request(request) + content = response.read() + response.close() + assert response.status == 200 + assert content == b"ok" + except Exception as exc: # noqa: BLE001 # pragma: no cover + with errors_lock: + errors.append(exc) + + threads = [ + threading.Thread(target=worker, args=(w,), name=f"h2-worker-{w}") + for w in range(n_threads) + ] + for thread in threads: + thread.start() + for thread in threads: + thread.join(timeout=120) + + assert not any(thread.is_alive() for thread in threads), "worker threads hung" + assert errors == [], ( + f"{len(errors)} request(s) failed out of {n_threads * n_requests}: " + f"{sorted({type(e).__name__ for e in errors})}" + ) diff --git a/tests/test_http2_send_cancellation.py b/tests/test_http2_send_cancellation.py new file mode 100644 index 000000000..85cd9508f --- /dev/null +++ b/tests/test_http2_send_cancellation.py @@ -0,0 +1,87 @@ +"""Cancellation while sending HTTP/2 request headers must release resources.""" + +import anyio +import pytest + +import httpcore +from httpcore._synchronization import AsyncSemaphore + + +@pytest.mark.anyio +async def test_http2_cancelled_header_write_releases_stream_capacity(): + origin = httpcore.Origin(b"https", b"example.com", 443) + conn = httpcore.AsyncHTTP2Connection( + origin=origin, stream=httpcore.AsyncMockStream([]) + ) + conn._h2_state.initiate_connection() + conn._sent_connection_init = True + conn._max_streams = 1 + conn._max_streams_semaphore = AsyncSemaphore(1) + paused_on_write = anyio.Event() + resume_write = anyio.Event() + + async def paused_write(request): + paused_on_write.set() + await resume_write.wait() + + conn._write_outgoing_data = paused_write + request = httpcore.Request( + "GET", "https://example.com/", headers=[(b"host", b"example.com")] + ) + + async def send(): + await conn.handle_async_request(request) + + async with anyio.create_task_group() as group: + group.start_soon(send) + with anyio.fail_after(1): + await paused_on_write.wait() + assert conn._events, "did not reach send after stream registration" + group.cancel_scope.cancel() + + # A cancellation after stream allocation must not strand the permit. + assert conn._events == {} + # Counts lifetime streams, including the cancelled stream that was allocated. + assert conn._request_count == 1 + with anyio.fail_after(1): + await conn._max_streams_semaphore.acquire() + await conn.aclose() + + +@pytest.mark.anyio +async def test_http2_cancel_before_stream_allocation_releases_capacity(): + origin = httpcore.Origin(b"https", b"example.com", 443) + conn = httpcore.AsyncHTTP2Connection( + origin=origin, stream=httpcore.AsyncMockStream([]) + ) + conn._h2_state.initiate_connection() + conn._sent_connection_init = True + conn._max_streams = 1 + conn._max_streams_semaphore = AsyncSemaphore(1) + paused_before_allocate = anyio.Event() + resume_allocate = anyio.Event() + + async def paused_headers(request): + paused_before_allocate.set() + await resume_allocate.wait() + raise AssertionError("should have been cancelled") + + conn._send_request_headers = paused_headers + request = httpcore.Request( + "GET", "https://example.com/", headers=[(b"host", b"example.com")] + ) + + async def send(): + await conn.handle_async_request(request) + + async with anyio.create_task_group() as group: + group.start_soon(send) + with anyio.fail_after(1): + await paused_before_allocate.wait() + group.cancel_scope.cancel() + + assert conn._events == {} + assert conn._request_count == 0 + with anyio.fail_after(1): + await conn._max_streams_semaphore.acquire() + await conn.aclose()