You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BodyPartReader.read_chunk() crashes with AssertionError on a body part with Content-Length: 0 #13758
self._length is int | None. A body part with an explicit Content-Length: 0 header (legal outside multipart/form-data, e.g. multipart/mixed or multipart/related, where the empty-part special case that nulls out _length does not apply) sets self._length = 0, which is falsy. The reader then falls through to _read_chunk_from_stream, which asserts the requested chunk size is at least the boundary length:
asyncdef_read_chunk_from_stream(self, size: int) ->bytes:
assert (
size>=self._boundary_len
), "Chunk size must be greater or equal than boundary length + 2"
Any caller asking read_chunk() for a chunk smaller than the boundary length hits that assert instead of getting back an immediate empty chunk, even though Content-Length: 0 already says there's nothing to read.
AssertionError: Chunk size must be greater or equal than boundary length + 2
Expected behavior
read_chunk() on a part with Content-Length: 0 returns b"" immediately regardless of the requested size, the same as it does for a nonzero Content-Length.
Logs/tracebacks
Traceback (most recent call last):
File "repro.py", line 30, in <module>
asyncio.run(main())
File "repro.py", line 27, in mainawait part.read_chunk(4)
File ".../aiohttp/multipart.py", line 392, in read_chunk
fresh =awaitself._read_chunk_from_stream(want)
File ".../aiohttp/multipart.py", line 451, in _read_chunk_from_stream
size >=self._boundary_len
AssertionError: Chunk size must be greater or equal than boundary length + 2
Python Version
$ python --versionPython 3.14.3
aiohttp Version
$ python -m pip show aiohttpName: aiohttpVersion: 3.14.3
Reading through BodyPartReader, every other use of self._length in multipart.py correctly checks is not None (_align_base64_chunk, _read_chunk_from_length's assert). read_chunk's if self._length: is the odd one out.
Activity
Fix BodyPartReader.read_chunk() on parts with Content-Length: 0