Skip to content

Commit 6b399eb

Browse files
lesnik512claude
andcommitted
fix(client): map RuntimeError → TransportError via is_closed, not substring
Both AsyncClient._terminal and Client._terminal mapped RuntimeError to TransportError by checking `"closed" in str(exc)`. This conflates two unrelated conditions: any RuntimeError whose message happens to contain "closed" (from any transport layer, plugin, or future httpx2 wording change) was mis-classified as TransportError; conversely, if httpx2 reworded its closed-client error to "shut down" or "disposed", the mapping would silently break. Replace with `self._httpx2_client.is_closed` — the same public attribute already used elsewhere in client.py for the borrowed-client teardown guards (lines 774, 784, 870, 880). Message-independent. Tests pin both directions: RuntimeError raised while the client is open propagates as-is (even when its message contains "closed"); RuntimeError raised after close (real httpx2 case) maps to TransportError. Closes audit Nit finding (client.py:135 + :852) from planning/audit/2026-06-07-deep-audit.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 049bfa9 commit 6b399eb

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎src/httpware/client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ async def _terminal(self, request: httpx2.Request) -> httpx2.Response:
132132
async with _httpx2_exception_mapper():
133133
response = await self._httpx2_client.send(request)
134134
except RuntimeError as exc:
135-
if "closed" in str(exc):
135+
if self._httpx2_client.is_closed:
136136
raise TransportError(str(exc)) from exc
137137
raise
138138
_raise_on_status_error(response)
@@ -850,7 +850,7 @@ def _terminal(self, request: httpx2.Request) -> httpx2.Response:
850850
with _httpx2_exception_mapper_sync():
851851
response = self._httpx2_client.send(request)
852852
except RuntimeError as exc:
853-
if "closed" in str(exc):
853+
if self._httpx2_client.is_closed:
854854
raise TransportError(str(exc)) from exc
855855
raise
856856
_raise_on_status_error(response)

‎tests/test_client_lifecycle.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from http import HTTPStatus
44

55
import httpx2
6+
import pytest
67

78
from httpware import AsyncClient
9+
from httpware.errors import TransportError
810

911

1012
async def test_aexit_closes_owned_httpx2_client() -> None:
@@ -44,3 +46,32 @@ async def test_aclose_is_idempotent_for_owned_client() -> None:
4446
# Second call must not raise — the boolean prevents a double-close on httpx2 internals.
4547
await client.aclose()
4648
assert client._httpx2_client.is_closed # noqa: SLF001
49+
50+
51+
async def test_runtimeerror_unrelated_to_close_propagates_unchanged() -> None:
52+
"""A RuntimeError NOT caused by client closure must propagate as-is, not get mapped to TransportError.
53+
54+
The OLD substring check `"closed" in str(exc)` would mis-classify any
55+
RuntimeError whose message happened to contain "closed".
56+
"""
57+
msg = "downstream proxy hiccup — closed connection reset by peer" # contains "closed" but client is open
58+
59+
def _handler(_request: httpx2.Request) -> httpx2.Response:
60+
raise RuntimeError(msg)
61+
62+
transport = httpx2.MockTransport(_handler)
63+
async with AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) as client:
64+
with pytest.raises(RuntimeError) as exc_info:
65+
await client.get("https://example.test/x")
66+
assert not isinstance(exc_info.value, TransportError), (
67+
f"RuntimeError was incorrectly mapped to TransportError: {exc_info.value!r}"
68+
)
69+
70+
71+
async def test_runtimeerror_after_aclose_maps_to_transporterror() -> None:
72+
"""After aclose(), sending raises a RuntimeError that should map to TransportError via is_closed."""
73+
# Use an owned client (no httpx2_client= arg) so aclose() also closes the httpx2 layer.
74+
client = AsyncClient()
75+
await client.aclose()
76+
with pytest.raises(TransportError):
77+
await client.get("https://example.test/x")

‎tests/test_client_sync.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,26 @@ def test_close_does_not_close_borrowed_httpx2_client() -> None:
339339
client.close()
340340
assert not underlying.is_closed
341341
underlying.close()
342+
343+
344+
def test_runtimeerror_unrelated_to_close_propagates_unchanged() -> None:
345+
"""A RuntimeError NOT caused by client closure must propagate as-is (sync mirror)."""
346+
msg = "downstream proxy hiccup — closed connection reset by peer"
347+
348+
def _handler(_request: httpx2.Request) -> httpx2.Response:
349+
raise RuntimeError(msg)
350+
351+
transport = httpx2.MockTransport(_handler)
352+
with Client(httpx2_client=httpx2.Client(transport=transport)) as client:
353+
with pytest.raises(RuntimeError) as exc_info:
354+
client.get("https://example.test/x")
355+
assert not isinstance(exc_info.value, TransportError)
356+
357+
358+
def test_runtimeerror_after_close_maps_to_transporterror() -> None:
359+
"""After close(), sending raises a RuntimeError that should map to TransportError via is_closed (sync)."""
360+
# Use an owned client (no httpx2_client= arg) so close() also closes the httpx2 layer.
361+
client = Client()
362+
client.close()
363+
with pytest.raises(TransportError):
364+
client.get("https://example.test/x")

0 commit comments

Comments
 (0)