Jump to solution
Verify

The Fix

Fix middleware traceback fetching on Python 3.8+, fix ResourceWarnings in TestClient, and fix CI build.

Based on closed Kludex/starlette issue #1131 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -217,15 +217,15 @@ def generate_html(self, exc: Exception, limit: int = 7) -> str: exc, capture_locals=True ) - frames = inspect.getinnerframes( - traceback_obj.exc_traceback, limit # type: ignore - )
repro.py
============================= test session starts ============================== platform linux -- Python 3.8.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: /build/source, configfile: setup.cfg plugins: asyncio-0.14.0, cov-2.10.1 collected 240 items [...] =================================== FAILURES =================================== _______________________________ test_debug_html ________________________________ def test_debug_html(): async def app(scope, receive, send): raise RuntimeError("Something went wrong") app = ServerErrorMiddleware(app, debug=True) client = TestClient(app, raise_server_exceptions=False) response = client.get("/", headers={"Accept": "text/html, */*"}) assert response.status_code == 500 assert response.headers["content-type"].startswith("text/html") tests/middleware/test_errors.py:42: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = {}, key = 'content-type' def __getitem__(self, key): return self._store[key.lower()][1] E KeyError: 'content-type' /nix/store/9bb3iygm8kkdzfsaj3j1j2q845nzja1p-python3.8-requests-2.25.0/lib/python3.8/site-packages/requests/structures.py:54: KeyError
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Apply the official fix\nFix middleware traceback fetching on Python 3.8+, fix ResourceWarnings in TestClient, and fix CI build.\nWhen NOT to use: Do not apply this fix if using a version of Python older than 3.8.\n\n

Why This Fix Works in Production

  • Trigger: ============================= test session starts ==============================
  • Mechanism: Fix middleware traceback fetching on Python 3.8+, fix ResourceWarnings in TestClient, and fix CI build.

Why This Breaks in Prod

  • Shows up under Python 3.8.7 in real deployments (not just unit tests).
  • Surfaces as: ============================= test session starts ==============================

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“### Checklist <!-- Please make sure you check all these items before submitting your bug report. --> - [x] The bug is reproducible against the latest release and/or master. - [x] There are no similar issues or pull requests to fix it yet. #”
Issue thread · issue description · source

Failure Signature (Search String)

  • ============================= test session starts ==============================

Error Message

Stack trace
error.txt
Error Message ------------- ============================= test session starts ============================== platform linux -- Python 3.8.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: /build/source, configfile: setup.cfg plugins: asyncio-0.14.0, cov-2.10.1 collected 240 items [...] =================================== FAILURES =================================== _______________________________ test_debug_html ________________________________ def test_debug_html(): async def app(scope, receive, send): raise RuntimeError("Something went wrong") app = ServerErrorMiddleware(app, debug=True) client = TestClient(app, raise_server_exceptions=False) response = client.get("/", headers={"Accept": "text/html, */*"}) assert response.status_code == 500 assert response.headers["content-type"].startswith("text/html") tests/middleware/test_errors.py:42: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = {}, key = 'content-type' def __getitem__(self, key): return self._store[key.lower()][1] E KeyError: 'content-type' /nix/store/9bb3iygm8kkdzfsaj3j1j2q845nzja1p-python3.8-requests-2.25.0/lib/python3.8/site-packages/requests/structures.py:54: KeyError

Minimal Reproduction

repro.py
============================= test session starts ============================== platform linux -- Python 3.8.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: /build/source, configfile: setup.cfg plugins: asyncio-0.14.0, cov-2.10.1 collected 240 items [...] =================================== FAILURES =================================== _______________________________ test_debug_html ________________________________ def test_debug_html(): async def app(scope, receive, send): raise RuntimeError("Something went wrong") app = ServerErrorMiddleware(app, debug=True) client = TestClient(app, raise_server_exceptions=False) response = client.get("/", headers={"Accept": "text/html, */*"}) assert response.status_code == 500 assert response.headers["content-type"].startswith("text/html") tests/middleware/test_errors.py:42: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = {}, key = 'content-type' def __getitem__(self, key): return self._store[key.lower()][1] E KeyError: 'content-type' /nix/store/9bb3iygm8kkdzfsaj3j1j2q845nzja1p-python3.8-requests-2.25.0/lib/python3.8/site-packages/requests/structures.py:54: KeyError

Environment

  • Python: 3.8.7

What Broke

Test failures occurred during package builds on NixOS and Fedora.

Fix Options (Details)

Option A — Apply the official fix

Fix middleware traceback fetching on Python 3.8+, fix ResourceWarnings in TestClient, and fix CI build.

When NOT to use: Do not apply this fix if using a version of Python older than 3.8.

Fix reference: https://github.com/kludex/starlette/pull/1132

Last verified: 2026-02-11. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not apply this fix if using a version of Python older than 3.8.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
  • Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.