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

fix: pathsend handling in middlewares #2616

Closed
wants to merge 3 commits into from

Conversation

gi0baro
Copy link
Contributor

@gi0baro gi0baro commented Jun 10, 2024

Summary

Closes #2613

It seems that when I introduced support for pathsend in #2435 I didn't check the included middlewares to also support the relevant ASGI messages. Affected Starlette versions 0.36.0 and onwards when running on a server supporting pathsend.

Note: this is still in draft since I'd like to plan some tests for the involved code; also the proposed solution isn't necessarily the best one, probably someone like @Kludex might review this and give his feedbacks before proceeding further.
Update: I added tests just to verify the code. In case you're unhappy with the implementation, we can refactor that and keep the tests.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch 3 times, most recently from d7b0358 to 6916ede Compare June 10, 2024 15:21
async with recv_stream:
async for message in recv_stream:
if message["type"] == "http.response.pathsend":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there! wouldn't it make sense to just read the file and stream it through here? Would be better for compatibility, no? Or maybe have some flag like "supports_message_pass_through"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the file from Python would be just the standard ASGI behaviour and defeat the whole pathsend idea (see for ref https://asgi.readthedocs.io/en/latest/extensions.html#path-send).

About the flag: I'm not sure I get the point around it. How should we evaluate such flag? pathsend messages will be returned by Starlette only if the relevant extension is present in the ASGI scope as the server supports it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about random middlewares out there, deriving from this BaseMiddleware that do not know pathsend. To keep those running, returning the message instead of bytes could be opt-in via a flag/class property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aersam but even if you subclass BaseMiddleware, there's no way you can interact with that code. If the middleware completely overrides __call__ then it should be compliant with ASGI messages IMHO.

@gi0baro gi0baro marked this pull request as ready for review June 12, 2024 16:09
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch 2 times, most recently from 271c3b5 to 2107b5e Compare June 12, 2024 16:13
@gi0baro gi0baro changed the title fix: pathsend handling in middlewares (#2613) fix: pathsend handling in middlewares Jun 12, 2024
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch from 2107b5e to 5b15ea9 Compare June 12, 2024 16:18
@Kludex Kludex added the hold Don't merge it label Jun 12, 2024
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch from 5b15ea9 to 4f01bf6 Compare June 12, 2024 16:25
@gi0baro
Copy link
Contributor Author

gi0baro commented Jun 24, 2024

@Kludex any updates on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the changes here just because the StreamingResponse is used in the BaseHTTPMiddleware?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. A Better implementation would probably be to refactor BaseHTTPMiddleware, but it is something I am not confident to do by myself given my knowledge of the codebase is still limited.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not making changes to the rest of the code source to make BaseHTTPMiddleware happy.

My recommendation for Granian users, and/or any server supporting http.response.pathsend is to replace BaseHTTPMiddleware for pure ASGI middleware. I don't want us to add more complexity to that code. That piece of code had too many issues over time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your position.

I think there are 2 major themes here though:

  • StreamingResponse doesn't support any message type other than http.response.body, which make sense given the intent of streaming responses, so unless an additional body message type is added to ASGI this is future-proof. But, since BaseHTTPMiddleware depends on that, this also means any application using that middleware or a derivate middleware won't support any other ASGI message in responses, which is something you might want to change to be future-proof on ASGI spec changes.
  • As of today Starlette states (due to the changes I made in Add support for ASGI pathsend extension #2435) to support http.response.pathsend, which is only partially true: as soon as the route involves a BaseHTTPMiddleware, GZIPMiddleware or a derivate of those in the middleware chain, then Starlette doesn't actually support pathsend.

The rationale behind this PR is not to address the first point, but rather the 2nd; as today experience with pathsend supportive servers with any Starlette/FastAPI application is practically dependant on which middlewares are implemented on the route returning a file response.

So given all of the above, if we can't find a proper way to address the pathsend issue, I'd rather suggest to revert #2435 and remove pathsend support completely until we find a proper way to support that ASGI extension in all the usages of Starlette and its dependants. This would sacrifice the performance gain of pathsend but at least we'd have the same DX across all the Starlette apps, regardless of the middlewares used, and thus prevent the whole bunch of issues related to this opened in Granian repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can revert that one. It will be easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll open a different PR later today and close this one

@gi0baro
Copy link
Contributor Author

gi0baro commented Jul 23, 2024

Closing this in favour of #2649, as discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hold Don't merge it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pathsend causing issues with BaseHTTPMiddleware
3 participants