Skip to content

Method "read_chunk" of "BodyPartReader" returns zero bytes before eof #1428

Closed
@viotti

Description

Long story short

I've implemented a multipart file upload handler inspired on code from the docs. My code is truncating part's data. I believe the problem is in the method _read_chunk_from_stream, which is used by read_chunk of BodyPartReader. That method is returning a zero-length bytearray before the part's EOF. This is the pseudo-code.

reader = await request.multipart()
part = await reader.next()
arr = bytearray()

while True:
    chunk = await part.read_chunk()  # 8192 bytes by default.

    if not chunk:
        break

    arr.extend(chunk)

Expected behaviour

The loop ends when all the part's data has been read.

Actual behaviour

The loop ends before the part's data is exhausted, i.e., chunk becomes a zero-length bytearray prematurely.

Steps to reproduce

The code is part of a large web application so it's hard for me to give reproducible steps. But replacing the break condition to if not part._at_eof made the problem go away.

reader = await request.multipart()
part = await reader.next()
arr = bytearray()

while True:
    chunk = await part.read_chunk()  # 8192 bytes by default.

    if not part._at_eof:  # This fixed the problem.
        break

    arr.extend(chunk)

Your environment

Aiohttp 1.1.5
Python 3.5.1 from PSF
macOS Sierra 10.12.1

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions