mongkok / fastapi-debug-toolbar

A debug toolbar for FastAPI.
https://fastapi-debug-toolbar.domake.io
BSD 3-Clause "New" or "Revised" License
141 stars 14 forks source link

h11._util.LocalProtocolError: Too little data for declared Content-Length when open FastAPI docs #10

Closed chrismaille closed 1 year ago

chrismaille commented 2 years ago

Dependencies:

Traceback

When trying to open http://localhost:XXXX/docs:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 364, 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 75, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 212, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, 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
  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/base.py", line 64, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 244, in __call__
    await wrap(partial(self.listen_for_disconnect, receive))
  File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 574, in __aexit__
    raise exceptions[0]
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 240, in wrap
    await func()
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 234, in stream_response
    await send({"type": "http.response.body", "body": b"", "more_body": False})
  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/h11_impl.py", line 468, in send
    output = self.conn.send(event)
  File "/usr/local/lib/python3.9/site-packages/h11/_connection.py", line 468, in send
    data_list = self.send_with_data_passthrough(event)
  File "/usr/local/lib/python3.9/site-packages/h11/_connection.py", line 501, in send_with_data_passthrough
    writer(event, data_list.append)
  File "/usr/local/lib/python3.9/site-packages/h11/_writers.py", line 60, in __call__
    self.send_eom(event.headers, write)
  File "/usr/local/lib/python3.9/site-packages/h11/_writers.py", line 83, in send_eom
    raise LocalProtocolError("Too little data for declared Content-Length")
h11._util.LocalProtocolError: Too little data for declared Content-Length

The issue (I think)

https://github.com/mongkok/fastapi-debug-toolbar/blob/3552b0bbb8e1a86a4f5eaaf214e6916d52c941ef/debug_toolbar/middleware.py#L84

On first iteration, body equals the correct body from response (b'<HTML CONTENT>'), on second iteration body is empty (b''). This empty body is use on the subsequent code, inserted on response, but line 97 never occurs.

andreipradan commented 2 years ago

@chrismaille Was this issue fixed in a different way? I've seen the related PR was closed and not merged

YaoJusheng commented 1 year ago

@mongkok

The same error occured in my code. Was this solution worked ?

mongkok commented 1 year ago

The issue was fixed, please try the latest release 0.3.2.