Skip to content

fix: forward messages sent before the response start in the cache middleware - #93

Merged
tishun merged 2 commits into
redis:mainfrom
davidpoblador:fix/debug-before-start
Nov 6, 2026
Merged

tishun merged 2 commits into
redis:mainfrom
davidpoblador:fix/debug-before-start

Conversation

@davidpoblador

Copy link
Copy Markdown
Contributor

Fixes #92.

Starlette's TemplateResponse sends http.response.debug before http.response.start when the server supports that extension, and TestClient does. The cache middleware took any non-body message as a signal to flush the buffered start, even when no start had arrived yet. As a result:

  • uncached template routes sent two starts, and TestClient failed with "Received multiple http.response.start messages"
  • cached template routes were served with no headers and never stored

Now the middleware forwards anything that arrives before the start as-is, and caching works as usual. Messages after the start are handled the same as before.

I added two tests next to the existing extension message tests and a short note in the architecture guide.

The previous commit stops the cache middleware making up a response
start when a message arrives before the real one. It still treats
http.response.debug as a message that carries content. That is wrong:
debug carries test metadata (the template and its context), not
content. When it arrives after the start, the middleware stops caching
a response it could store.

Forward debug as soon as it arrives and leave buffering alone. This
order is safe. Starlette's TemplateResponse already sends debug before
the start, and TestClient accepts debug at any point.

Tests:
- Add unit tests that need no Redis and use the real Jinja2Templates
  under TestClient. They fail on the code from before redis#92 was fixed,
  and they also fail if Starlette changes the order it sends messages.
- Take debug out of the group of messages that replace the body. Those
  tests sent a start, then debug, and no body, which no real server
  or response does.
- Check scope["extensions"] before the fake response sends debug, as
  Starlette does.
- Cover debug before and after the start, on uncached and cached routes.
- Add jinja2 to the test dependency group. Before this, only the docs
  tools installed it, and the CI test sessions don't install those.

Docs:
- Remove debug from the list of responses the middleware cannot buffer.
- Tell users that a cache hit renders no template, so TestClient
  reports no response.template or response.context for it.

Refs redis#92
@tishun

tishun commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Hey @davidpoblador thanks for this contribution!

I extended your solution a bit, but it is mostly based on your work. Check out the commit message for details.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.38%. Comparing base (d72b161) to head (e18669c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #93      +/-   ##
==========================================
- Coverage   97.45%   97.38%   -0.07%     
==========================================
  Files          15       15              
  Lines        2634     2642       +8     
==========================================
+ Hits         2567     2573       +6     
- Misses         67       69       +2     
Flag Coverage Δ
integration 77.21% <100.00%> (-0.01%) ⬇️
unit 95.45% <100.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tishun
tishun merged commit b4f0285 into redis:main Nov 6, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cached routes crash under Starlette's TestClient when the response is a template

3 participants