Skip to content

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

Closed

Description

@davidpoblador

Hi! I've been using fastapi-redis-sdk 0.9.1 with route caching, and pages rendered with Jinja templates fail under Starlette's TestClient. The test client reports two response starts for the same request, and the page comes back as a 500.

From what I can tell, the cache middleware assumes every message other than the body arrives after http.response.start. Starlette's TemplateResponse sends an http.response.debug message before the start whenever the server advertises that extension, and TestClient does. The middleware treats that debug message as "flush the buffered start now", but nothing has been buffered yet. So the client gets a start it shouldn't, and then the real one.

It doesn't happen with servers that don't advertise the debug extension (Granian, in our case), so it mostly bites in tests.

A fix could be to pass any message that arrives before http.response.start straight through, without flushing anything. We work around it for now with a small middleware that drops the debug messages before they reach the cache.

Happy to send a PR if that helps. Thanks for the library!

Activity

  1. tishun commented on Oct 24, 2026

    @tishun
    Collaborator

    Thanks for the bug report!

    All contributions are very welcome, but if you do not have the time the team will address it the moment we have some time.

  2. added theissue type on Oct 24, 2026
  3. added
    needs-triageThe team needs to go through the provided information and analyse it
    and removed
    bugSomething isn't working
    needs-triageThe team needs to go through the provided information and analyse it
    on Oct 24, 2026
  4. davidpoblador commented on Oct 25, 2026

    @davidpoblador
    ContributorAuthor

    Opened #93 with a fix. One correction to my report: the crash happens on uncached template routes. Cached ones don't crash, but they lose their headers and never get stored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions