granian: AssertionError with FastAPI and Granian when using Middleware
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 .
AssertionError:
[ERROR] Application callable raised an exception
Traceback (most recent call last):
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\granian\_futures.py", line 4, in future_watcher
await inner(watcher.scope, watcher.proto)
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\applications.py", line 123, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\middleware\base.py", line 189, in __call__
with collapse_excgroups():
File "C:\Users\Geo\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 155, in __exit__
self.gen.throw(typ, value, traceback)
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
raise exc
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\responses.py", line 260, in wrap
await func()
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\middleware\base.py", line 217, in stream_response
return await super().stream_response(send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\responses.py", line 249, in stream_response
async for chunk in self.body_iterator:
File "C:\Users\Geo\AppData\Local\pypoetry\Cache\virtualenvs\website-v-for-vets-QDVzixt4-py3.11\Lib\site-packages\starlette\middleware\base.py", line 173, in body_stream
assert message["type"] == "http.response.body"
Here are the dependencies of my environment:
- Python version: 3.11
- FastAPI version: 0.109.2
- Granian version: 1.1.0 (with reload extra)
- Uvicorn version: 0.27.1
- Other relevant packages:
- jinja2-fragments: 1.2.1
- loguru: 0.7.2
- slowapi: 0.1.9
- gunicorn: 21.2.0
- pydantic-settings: 2.2.0
- supabase: 2.3.5
Thanks in advance!
Funding
- You can sponsor this specific effort via a Polar.sh pledge below
- We receive the pledge once the issue is completed & verified
About this issue
- Original URL
- State: closed
- Created 4 months ago
- Comments: 24 (13 by maintainers)
Commits related to this issue
- Avoid errors raise on `PyFutureResultSetter` calls (#216) — committed to emmett-framework/granian by gi0baro 3 months ago
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.
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
@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.