fix(proxy): import Response at runtime in the Bedrock handler

handle_bedrock_invoke returns Response(status_code=204) when the client
disconnects during the body read, but Response was only imported under
TYPE_CHECKING, so that branch raised NameError. Import it next to
JSONResponse inside the handler and add a regression test.

Fixes #4393

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 19:12:27 +07:00
co-authored by Claude Opus 5.5
parent e995eb9f1d
commit c7d9d59e0a
2 changed files with 25 additions and 1 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ class BedrockHandlerMixin:
URL path (may contain ``.``, ``:`` and ``/``).
stream: ``True`` for ``invoke-with-response-stream``.
"""
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, Response
from headroom.proxy.auth_mode import classify_client
from headroom.proxy.helpers import (
@@ -352,6 +352,30 @@ def test_oversized_body_returns_413_and_never_forwards(monkeypatch):
http.send.assert_not_called()
def test_client_disconnect_during_body_read_returns_204(monkeypatch):
"""A client that hangs up mid-body must not crash the handler: the
ClientDisconnect branch returns an empty 204 and never forwards.
"""
from starlette.requests import ClientDisconnect
import headroom.proxy.helpers as helpers_mod
async def _disconnect(request):
raise ClientDisconnect()
monkeypatch.setattr(helpers_mod, "read_request_json_with_bytes", _disconnect)
app = create_app(_make_config())
with TestClient(app) as client:
proxy = client.app.state.proxy
http = _install_fake_client(proxy, _FakeUpstream())
resp = client.post(INVOKE, json={"messages": [], "max_tokens": 8})
assert resp.status_code == 204
http.build_request.assert_not_called()
http.send.assert_not_called()
@pytest.mark.parametrize(
"route", [INVOKE, INVOKE_STREAM], ids=["invoke", "invoke-with-response-stream"]
)