aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

EmptyStreamReader.total_raw_bytes raises AttributeError on a response with no body

Open chrikrah opened this issue 1 month ago • 1 comments

Describe the bug

resp.content.total_raw_bytes raises AttributeError when the response has no body, such as a 204 or a reply to HEAD. EmptyStreamReader.__init__ skips StreamReader.__init__. It sets total_bytes but not total_compressed_bytes, which the property reads. #10386 was the same omission for total_bytes.

The property is documented as a read-only int at docs/streams.rst:117. A progress loop that reads it after the response ends fails on exactly the responses that have no body.

To Reproduce

import asyncio
import aiohttp
from aiohttp import web
from aiohttp.test_utils import TestServer

async def handler(request):
    return web.Response(status=204)

async def main():
    app = web.Application()
    app.router.add_get("/", handler)
    async with TestServer(app) as server:
        async with aiohttp.ClientSession() as session:
            async with session.get(server.make_url("/")) as resp:
                print("total_bytes     ", resp.content.total_bytes)
                print("total_raw_bytes ", resp.content.total_raw_bytes)

asyncio.run(main())

Expected behavior

total_raw_bytes returns 0, as total_bytes does.

Logs/tracebacks

total_bytes      0
  File ".../aiohttp/streams.py", line 262, in total_raw_bytes
    if self.total_compressed_bytes is None:
AttributeError: 'EmptyStreamReader' object has no attribute 'total_compressed_bytes'

Python Version

$ python --version
Python 3.12.6

aiohttp Version

$ python -m pip show aiohttp
Version: 3.14.5   # also master at 7275bea (4.0.0a2.dev0), pure Python

multidict Version

$ python -m pip show multidict
Version: 7.1.0

propcache Version

$ python -m pip show propcache
Version: 0.5.4

yarl Version

$ python -m pip show yarl
Version: 1.25.1

OS

Linux

Related component

Client

Additional context

One line fixes it, next to the total_bytes line #10386 added: self.total_compressed_bytes = None in EmptyStreamReader.__init__. With it, the script above prints 0 for both on master at 7275bea.

I found it while reviewing #14078. The chunk hooks and get_read_buffer_limits() also raise there, but they need a different fix. Setting _http_chunk_splits would let begin_http_chunk_receiving() park a deque on the shared EMPTY_PAYLOAD, which streams.py:617 guards against, so they want no-op overrides and constant limits. I'll keep those out of it.

@bdraco, I'm opening the one-liner with a 204 and HEAD test next.

Code of Conduct

  • [x] I agree to follow the aio-libs Code of Conduct

chrikrah avatar Oct 22 '26 01:10 chrikrah

I'd like to work on this — same omission pattern as #10386: EmptyStreamReader.__init__ sets total_bytes but not total_compressed_bytes.

rinopatrick avatar Nov 01 '26 18:11 rinopatrick