Error using starlette.responses.StreamingResponse
sralloza opened this issue · comments
Diego Alloza González commented
Using this simple fastapi app converted into wsgi:
from io import BytesIO
from a2wsgi import ASGIMiddleware
from fastapi import FastAPI
from starlette.responses import StreamingResponse
from werkzeug import run_simple
app = FastAPI()
data = b"a" * 5 * 1024
@app.get("/")
def index():
buffer = BytesIO(data)
return StreamingResponse(buffer, media_type="application/octet-stream", headers={"content-disposition": "filename=a.png"})
application = ASGIMiddleware(app)
if __name__ == "__main__":
run_simple("localhost", 8000, application)
You can see this error is raised:
* Running on http://localhost:8000/ (Press CTRL+C to quit)
Task was destroyed but it is pending!
task: <Task pending coro=<FastAPI.__call__() done, defined at d:\Sistema\Desktop\test\.venv\lib\site-packages\fastapi\applications.py:184> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000018624DCF138>()]> cb=[ASGIResponder.__call__.<locals>._done_callback() at d:\Sistema\Desktop\test\.venv\lib\site-packages\a2wsgi\asgi.py:145]>
Task was destroyed but it is pending!
task: <Task pending coro=<StreamingResponse.stream_response() done, defined at d:\Sistema\Desktop\test\.venv\lib\site-packages\starlette\responses.py:212> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000018624DCF288>()]> cb=[_wait.<locals>._on_completion() at c:\python\lib\asyncio\tasks.py:436]>
Task was destroyed but it is pending!
task: <Task pending coro=<StreamingResponse.listen_for_disconnect() done, defined at d:\Sistema\Desktop\test\.venv\lib\site-packages\starlette\responses.py:206> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000018624DCF168>()]> cb=[_wait.<locals>._on_completion() at c:\python\lib\asyncio\tasks.py:436]>
127.0.0.1 - - [30/Nov/2020 14:01:17] "GET / HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
File "d:\Sistema\Desktop\test\.venv\lib\site-packages\werkzeug\serving.py", line 323, in run_wsgi
execute(self.server.app)
File "d:\Sistema\Desktop\test\.venv\lib\site-packages\werkzeug\serving.py", line 315, in execute
write(data)
File "d:\Sistema\Desktop\test\.venv\lib\site-packages\werkzeug\serving.py", line 265, in write
assert headers_set, "write() before start_response"
AssertionError: write() before start_response
If you execute the app using uvicorn (asgi), using this command,
uvicorn main:app
The application works just fine and the browser downloads the file a.png
.
Aber commented
This BUG is due to the fact that two coroutines perform the set
operation on self.sync_event at the same time, causing the message of http.response.start
to be lost.
It is a little difficult to fix it. It seems that the existing SyncEvent
design cannot be solved. I need some time or help.
Aber commented
This problem has been resolved in a2wsgi 1.3.1. Thanks for your feedback.