sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18.11k stars 1.55k forks source link

Response streaming produces [ERROR] Invalid response type None (need HTTPResponse) #2911

Closed ekzhang closed 7 months ago

ekzhang commented 9 months ago

Is there an existing issue for this?

Describe the bug

The "response streaming" feature of Sanic produces error messages when running from Uvicorn.

When accessing a page using the await request.respond() API, it produces error messages after each request.

[2024-01-31 19:37:14 +0000] [694830] [INFO] 
  ┌─────────────────────────────────────────────────────────────────────────────────┐
  │                                  Sanic v23.6.0                                  │
  │                                                                                 │
  ├───────────────────────┬─────────────────────────────────────────────────────────┤
  │                       │     mode: production, ASGI                              │
  │     ▄███ █████ ██     │   server: ASGI                                          │
  │    ██                 │   python: 3.11.6                                        │
  │     ▀███████ ███▄     │ platform: Linux-5.15.0-1048-aws-x86_64-with-glibc2.31   │
  │                 ██    │ packages: sanic-routing==23.12.0, sanic-testing==23.6.0 │
  │    ████ ████████▀     │                                                         │
  │                       │                                                         │
  │ Build Fast. Run Fast. │                                                         │
  └───────────────────────┴─────────────────────────────────────────────────────────┘

INFO:     Application startup complete.
INFO:     127.0.0.1:42186 - "GET / HTTP/1.1" 200 OK
[2024-01-31 19:38:19 +0000] [694830] [ERROR] Invalid response type None (need HTTPResponse)
Traceback (most recent call last):
  File "handle_request", line 144, in handle_request
    "_inspector",
            ^^^^^
sanic.exceptions.ServerError: Invalid response type None (need HTTPResponse)
[2024-01-31 19:38:19 +0000] [694830] [ERROR] The error response will not be sent to the client for the following exception:"Invalid response type None (need HTTPResponse)". A previous response has at least partially been sent.

Code snippet


from sanic import Sanic

app = Sanic("my-hello-world-app")

@app.route("/")
async def test(request):
    response = await request.respond(content_type="text/plain")
    await response.send("hello world")
    await response.eof()

if __name__ == "__main__":
    app.run()

Expected Behavior

Sanic should not produce error messages when using the response streaming API.

How do you run Sanic?

ASGI

Operating System

Linux

Sanic Version

23.6.0

Additional context

Possibly related to #2572, but it seems like a different issue. I can reproduce this without using WebSockets or SSE.