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

Bug: Setting response_headers on the app triggers exceptions #2196

Closed
1 of 4 tasks
Alc-Alc opened this issue Aug 22, 2023 · 0 comments · Fixed by #2308
Closed
1 of 4 tasks

Bug: Setting response_headers on the app triggers exceptions #2196

Alc-Alc opened this issue Aug 22, 2023 · 0 comments · Fixed by #2308
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@Alc-Alc
Copy link
Contributor

Alc-Alc commented Aug 22, 2023

Description

Original issue reported by user 'tkoddi' in Discord. Creating a bug report here for better tracking.

After a couple of GET requests to the route, the following code raises the exception shown under "Logs".

URL to code causing the issue

https://discord.com/channels/919193495116337154/1141668061406036039/1142135403369996298

MCVE

import uvicorn

from litestar import Litestar, get
from litestar.datastructures import ResponseHeader
from litestar.testing import TestClient


def generate_values():
    yield "a"
    yield "ab"


value_generator = generate_values()


@get("/")
async def handler() -> str:
    return next(value_generator)


app = Litestar(
    route_handlers=[handler],
    response_headers=[ResponseHeader(name="foo", value="bar")],
)


with TestClient(app) as client:
    assert client.get("/").headers == {"foo": "bar", "content-type": "text/plain; charset=utf-8", "content-length": "1"}
    assert client.get("/").headers == {"foo": "bar", "content-type": "text/plain; charset=utf-8", "content-length": "2"}

Steps to reproduce

1. `python mcve.py`

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

ERROR - 2023-08-18 16:11:23,147169275 - litestar - middleware - exception raised on http connection to route /v1/presets

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length
Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length
ERROR - 2023-08-18 16:11:23,147169277 - litestar - middleware - exception raised on http connection to route /v1/presets

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/authentication.py", line 78, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/base.py", line 129, in wrapped_call
    await original__call__(self, scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/compression.py", line 155, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.
Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/authentication.py", line 78, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/base.py", line 129, in wrapped_call
    await original__call__(self, scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/compression.py", line 155, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.
ERROR - 2023-08-18 16:11:23,147169280 - litestar - middleware - exception raised on http connection to route /v1/presets

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/base.py", line 129, in wrapped_call
    await original__call__(self, scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/compression.py", line 155, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/_asgi/asgi_router.py", line 84, in __call__
    await asgi_app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.
Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/authentication.py", line 78, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/base.py", line 129, in wrapped_call
    await original__call__(self, scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/compression.py", line 155, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/_asgi/asgi_router.py", line 84, in __call__
    await asgi_app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/routes/http.py", line 83, in handle
    await response(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 177, in __call__
    await self.send_body(send=send, receive=receive)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 158, in send_body
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 560, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/authentication.py", line 78, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/base.py", line 129, in wrapped_call
    await original__call__(self, scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/compression.py", line 155, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/_asgi/asgi_router.py", line 84, in __call__
    await asgi_app(scope, receive, send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 511, in __call__
    await self.asgi_handler(scope, receive, self._wrap_send(send=send, scope=scope))  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 173, in __call__
    await self.handle_request_exception(
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/middleware/exceptions/middleware.py", line 203, in handle_request_exception
    await response.to_asgi_response(app=litestar_app, request=request)(scope=scope, receive=receive, send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 171, in __call__
    await self.start_response(send=send)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/response/base.py", line 142, in start_response
    await send(event)
  File "/redacted/path/venv/lib/python3.11/site-packages/litestar/app.py", line 788, in wrapped_send
    await send(message)
  File "/redacted/path/venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 541, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Expected ASGI message 'http.response.body', but got 'http.response.start'.

Litestar Version

2.0.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Funding

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
Fund with Polar
@Alc-Alc Alc-Alc added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage labels Aug 22, 2023
@provinzkraut provinzkraut removed the Triage Required 🏥 This requires triage label Aug 22, 2023
@provinzkraut provinzkraut self-assigned this Aug 29, 2023
provinzkraut added a commit that referenced this issue Sep 17, 2023
…2308)

* fix headers handling in ASGIResponse

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants