-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
AssertionError with FastAPI and Granian when using Middleware #216
Comments
Hi @GeoKafkias, I'm not sure given the information you provided this is an issue with Granian. From the stack-trace you provided, it seems starlette is complaining about some ASGI response messages your application is returning, in fact if Granian had any complains about that you'll probably end up seeing some protocol exceptions raised by Granian code. Does the same works with uvicorn? If so, I ask you to provide some more context on what the applications is doing on that specific request. Also, I wonder if without the middlewares you mentioned it works fine or not. |
Hey @gi0baro, Thanks for your response. I forgot to mention that everything works fine when I am using unicorn. Also, I tested the case when I am not using middlewares at all where Granian works as expected but when I activate them I get the same error. |
@GeoKafkias can you identify which Middleware raises the error? Like if you can activate them one by one it might be helpful identifying which one or which combination is producing the issue |
Also, I'd love some advice from @Kludex on this :) |
This is not a Granian issue, it's a Starlette one. Feel free to close it. 👍 |
@Kludex thank you for the quick reply. Do you have any tracked issue/discussion on Starlette repo I can mention here before closing this? |
No, we don't have an issue yet. Someone needs to create a discussion, and provide some MRE yet. What is probably happening is that the message is |
@Kludex thank you for the explanation. I think that makes sense, it probably happens with Granian only 'cause the concurrency flow might be a bit different from uvicorn in dealing with recv/send messages. @GeoKafkias please open a discussion on Starlette repo as @Kludex suggested, you can mention this issue as a starting point. I'm closing this, feel free to mention me in Starlette repo if you need anything from my side. |
(I think it does happen with Uvicorn as well... Not 100% sure, but I saw this issue being mentioned elsewhere...) |
Hey Both, Thanks for your help. Uvicorn is functioning as expected; I am confident about this (100% sure). However, I'm encountering issues when integrating granian—regardless of the various middleware combinations I've experimented with, errors persist exclusively in the presence of granian for both mentioned middelwares or one by one. |
The following code can reproduce this problem. I don't care whether it is caused by Starlette or Granian, but if this problem persists, I can only continue to use Uvicorn or Gunicorn and cannot migrate to Granian. I did not encounter this problem when using Uvicorn and Gunicorn
|
@wf1314 @GeoKafkias I gonna spend some time reproducing this locally and do some debugging later today |
@wf1314 FYI I can reproduce your issue locally with Python 3.10. Interestingly, it doesn't happen with Python 3.11. |
@wf1314 it is also worth noting, even if Granian is reporting that |
(Oi, sorry. I was wrong, and it only happens with granian? Just to check) |
Update on @wf1314 The involved Starlette code is https://github.com/encode/starlette/blob/0.36.3/starlette/responses.py#L257-L264 async with anyio.create_task_group() as task_group:
async def wrap(func: typing.Callable[[], typing.Awaitable[None]]) -> None:
await func()
task_group.cancel_scope.cancel()
task_group.start_soon(wrap, partial(self.stream_response, send))
await wrap(partial(self.listen_for_disconnect, receive)) Now, I don't exactly know what's wrong, but clearly something is different in handling cancellation with Below are the tests (some debugging info added on Granian, the HERE was added before this Starlette line https://github.com/encode/starlette/blob/0.36.3/starlette/middleware/base.py#L52): Python 3.10
Python 3.11
Python 3.12
So my position on this @wf1314 is that there's nothing Granian can do to solve it. Also 'cause there's nothing changing between Python versions in Granian
Also, under my perspective, this happens under Granian and not Uvicorn just because Granian is effectively more concurrent (async send and receive are handled in Rust separated threads, and not 1 by 1 from the single thread of the Python event loop), and thus any code that coincidentally was written taking into account consequentiality in tasks/steps happening in Python will fail in Granian. Regarding the original issue reported by @GeoKafkias, I'll perform some specific debugging later to actually get the culprit. |
@GeoKafkias I can't reproduce your issue locally. I suspect it might actually related to Windows. Do you have any chance to test the same code with a different platform? @wf1314 just another consideration, you might also want to wait for Granian 1.2 that will introduce some major refactors due to PyO3 API changes and check if anything changes on Python 3.10 with your stack. But there's no ETA and no assurance it will do make any difference. |
@gi0baro |
Hey @gi0baro, I don't know if this info plays any role but I forgot to mention that the response class of the requests is HTMLResponse. I still have the issue in the Ubuntu environment. Python version for Windows is 3.11.5 and for Ubuntu 3.11.4 and I am using poetry for python packaging and dependency management. Here is my code:
and the traceback:
Let me know if I can do something else. |
@GeoKafkias @wf1314 can you both please try again with 1.2.0 and report any updates on this? |
Hey @gi0baro , I updated and tested the newest version (1.2.0), but I still get some errors.
and here is the old error:
|
@GeoKafkias thank you for the update. Are you also able to check the same on Linux/Mac? |
Hello @gi0baro, I haven't had a chance to review version 1.2 in the Linux environment yet. I plan to do so later this week when I have some free time. |
Closing this due to changes in 1.3 |
Hey,
Firstly, I would like to express my gratitude for developing this exceptional package and your effort in it. I am fairly new to this area, but I am considering leveraging Granian's power for my FastAPI project. A little background: I am integrating various middleware (SlowAPIMiddelware & GZipMiddleware) into my FastAPI application, but I have encountered an issue that I hope to get some guidance on.
When running my FastAPI with Granian, I am facing an "AssertionError" linked to middleware processing when the application asserts the message type as "http.response.body". Check Error Traceback
.
Here are the dependencies of my environment:
Thanks in advance!
Funding
The text was updated successfully, but these errors were encountered: