Skip to content

Gzip middleware randomly raise RuntimeError but does not affect use , what are the possible causes? #2818

@GoodManWEN

Description

@GoodManWEN

Example

Here's a self-contained, minimal, reproducible, example with my use case:

from fastapi import FastAPI 
from fastapi.staticfiles import StaticFiles
from .pages import handerls

app = FastAPI(docs_url=None, redoc_url=None)
templates = Jinja2Templates(directory="templates")
handlers.install(templates)
app.mount('/', StaticFiles(directory='./static'), name='static')

Description

Hi everybody , I'm having problems with the gzip middleware recommended by the official docs .I'm developing a personal web application using vue as front-end framework , and fastapi needs to serve as the web engine for those packaged front-end js&css program files , and meanwhile fastapi should supports some back-end functional API.

After my service was deployed, I frequently encountered some RuntimeErrors in the log files , but these errors were not affecting the use at all , I can't feel the error in my use of the app at all ,but the error just pops up frequently in the logs ,which is very annoying. Since the error does not involve my business code at all, I am not sure why this is happening. Can anyone help me to see what the cause of the error might be ,thanks.

Error logs

After deployment, the logs usually look like this , as you can see, it often has some error messages mixed in with normal usage logs:

INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
INFO:     Started reloader process [6830] using watchgod
INFO:     Started server process [6832]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:51894 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:51896 - "GET /css/chunk-vendors.62464221.css HTTP/1.1" 200 OK
INFO:     127.0.0.1:51898 - "GET /css/app.d399f315.css HTTP/1.1" 200 OK
INFO:     127.0.0.1:51900 - "GET /js/app.7c9219d3.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:51902 - "GET /js/chunk-vendors.3c9cb68c.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:51904 - "GET /img/Material-design-background-514054880_2126x14162.jpg HTTP/1.1" 200 OK
INFO:     127.0.0.1:51906 - "GET /fonts/materialdesignicons-webfont.woff2 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51908 - "GET /fonts/KFOlCnqEu92Fr1MmEU9fBBc4.woff2 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51910 - "GET /fonts/KFOmCnqEu92Fr1Mu4mxK.woff2 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51912 - "GET /img/material.jpg HTTP/1.1" 304 Not Modified
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 396, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 18, in __call__
    await responder(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 35, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 223, in stream_response
    await send({"type": "http.response.body", "body": chunk, "more_body": True})
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 78, in send_with_gzip
    await self.send(message)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 156, in _send
    await send(message)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 522, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length
INFO:     127.0.0.1:51914 - "GET /img/bg/random?mode=light HTTP/1.1" 307 Temporary Redirect
INFO:     127.0.0.1:51916 - "GET /img/bg/light/844-1920x1080.jpg HTTP/1.1" 200 OK
INFO:     127.0.0.1:51918 - "GET /favicon.ico HTTP/1.1" 200 OK
INFO:     127.0.0.1:51920 - "GET /3 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51922 - "GET /4 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51924 - "GET /2 HTTP/1.1" 200 OK
INFO:     127.0.0.1:51926 - "GET /szzs HTTP/1.1" 200 OK
INFO:     127.0.0.1:51928 - "GET /favicon.ico HTTP/1.1" 200 OK
INFO:     127.0.0.1:51930 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:51932 - "GET /css/app.d399f315.css HTTP/1.1" 304 Not Modified
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 396, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 18, in __call__
    await responder(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 35, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 223, in stream_response
    await send({"type": "http.response.body", "body": chunk, "more_body": True})
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 78, in send_with_gzip
    await self.send(message)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 156, in _send
    await send(message)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 522, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length
INFO:     127.0.0.1:51934 - "GET /js/app.7c9219d3.js HTTP/1.1" 304 Not Modified
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 396, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 18, in __call__
    await responder(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 35, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 223, in stream_response
    await send({"type": "http.response.body", "body": chunk, "more_body": True})
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 78, in send_with_gzip
    await self.send(message)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 156, in _send
    await send(message)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 522, in send
    raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length
INFO:     127.0.0.1:51936 - "GET /js/chunk-vendors.3c9cb68c.js HTTP/1.1" 304 Not Modified
INFO:     127.0.0.1:51938 - "GET /css/chunk-vendors.62464221.css HTTP/1.1" 304 Not Modified

Environment

  • OS: Linux ubuntu20.04 / Windows 10
  • Python: 3.8.7 / 3.9.1
  • FastAPI Version [e.g. 0.3.0]: 0.63.0

Both run into the same problem with two of my machines

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions