Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathsend causing issues with BaseHTTPMiddleware #2613

Closed
aersam opened this issue Jun 10, 2024 · 6 comments
Closed

pathsend causing issues with BaseHTTPMiddleware #2613

aersam opened this issue Jun 10, 2024 · 6 comments
Assignees

Comments

@aersam
Copy link

aersam commented Jun 10, 2024

Hi guys

This is a follow up of emmett-framework/granian#320

I'm not entirely sure if this is really a bug of starlette, or more of a feature that's missing or so.

On my staging environment, I did receive an assertion error:

 File "/tmp/8dc89326e92b51b/antenv/lib/python3.12/site-packages/starlette/middleware/base.py", line 173, in body_stream
2024-06-10T09:58:53.052818649Z     assert message["type"] == "http.response.body"

As I could not reproduce it locally, I monkey-patched starlette and printed an error message with details:

2024-06-10T09:58:53.052814649Z   File "/tmp/8dc89326e92b51b/antenv/lib/python3.12/site-packages/starlette/middleware/base.py", line 173, in body_stream
2024-06-10T09:58:53.052818649Z     assert message["type"] == "http.response.body", f"Unexpected message of type {message['type']} from class {type(self)}: {message}"  # noqa: E501
2024-06-10T09:58:53.052821649Z     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-10T09:58:53.052824449Z AssertionError: Unexpected message of type http.response.pathsend from class <class 'starlette.middleware.base.BaseHTTPMiddleware'>: {'type': 'http.response.pathsend', 'path': 'frontend_app/output/index.html'}

I think this happens because my server does support the pathsend extension, however middlware that act's on it and just derives from BaseHTTPMiddleware will fail, since it cannot handle this.

Important

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@gi0baro
Copy link
Contributor

gi0baro commented Jul 23, 2024

This is mitigated by #2649, which is released in 0.38.1.

@Kludex let me know if you prefer to keep this open for the pathsend support general issue, or if I should open up a different issue to track it.

@Kludex
Copy link
Member

Kludex commented Aug 1, 2024

@gi0baro We are considering deprecating BaseHTTPMiddleware, and removing for 1.0. Only considering for now.

In any case, we don't recommend using BaseHTTPMiddleware for a long time. What we can do is:

  1. Add the pathsend support back.
  2. Add a conditional on the GzipMiddleware.
  3. Recommend Granian users to not use BaseHTTPMiddleware - You can tell people that the Starlette maintainer said that.

Wdyt?

About the GzipMiddleware, I want to avoid adding a conditional every time we have a new extension created, so maybe we can change some conditional (changing an else for a more specific elif?)?

@Kludex
Copy link
Member

Kludex commented Aug 1, 2024

In any case, I don't want to add more stuff to the BaseHTTPMiddleware, so we can mark this issue as "won't fix".

@gi0baro
Copy link
Contributor

gi0baro commented Aug 1, 2024

What we can do is:

  1. Add the pathsend support back.

Sure, I'll prepare a PR for that.

  1. Add a conditional on the GzipMiddleware.
    About the GzipMiddleware, I want to avoid adding a conditional every time we have a new extension created, so maybe we can change some conditional (changing an else for a more specific elif?)?

So instead of an elif like this https://github.com/encode/starlette/pull/2616/files#diff-0457610aa5a32911721d8869dfafdd471bbb89378197914246e291896dd312efR109, you'd prefer an else, did I get it right? If that's the case, makes sense to me.

  1. Recommend Granian users to not use BaseHTTPMiddleware - You can tell people that the Starlette maintainer said that.

Maybe we can also add a line in the documentation here (https://www.starlette.io/middleware/#basehttpmiddleware) in the limitations section, wdyt @Kludex?

@Kludex
Copy link
Member

Kludex commented Aug 6, 2024

Maybe we can also add a line in the documentation here (starlette.io/middleware#basehttpmiddleware) in the limitations section, wdyt @Kludex?

What do you want to add there?

Sure, I'll prepare a PR for that.

👍

So instead of an elif like this #2616 (files), you'd prefer an else, did I get it right? If that's the case, makes sense to me.

I changed my mind. Your elif was fine. Better to be explicit.

@Kludex Kludex closed this as not planned Won't fix, can't repro, duplicate, stale Aug 6, 2024
@gi0baro
Copy link
Contributor

gi0baro commented Aug 6, 2024

Maybe we can also add a line in the documentation here (starlette.io/middleware#basehttpmiddleware) in the limitations section, wdyt @Kludex?

What do you want to add there?

I would make it look like this:

Currently, the BaseHTTPMiddleware has some known limitations:

  • Using BaseHTTPMiddleware will prevent changes to contextlib.ContextVars from propagating upwards [...]
  • Using BaseHTTPMiddleware will prevent ASGI pathsend extension to work properly. Thus, if you run your Starlette application with a server implementing this extension, routes returning FileResponse should avoid the usage of this middleware.

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 a pull request may close this issue.

3 participants