Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Old method upload #535

Closed
wants to merge 3 commits into from
Closed

Fix: Old method upload #535

wants to merge 3 commits into from

Conversation

1yam
Copy link
Collaborator

@1yam 1yam commented Dec 19, 2023

Problem:
We require the old upload method to remain functional. In some cases, it may crash due to the HTTP client not setting the multipart header for the file_field.

Solution:
If no message is sent, we don't need to know the file size. We can simply read chunks until we reach the maximum size for unauthenticated uploads or end of file.

Copy link
Member

@MHHukiewitz MHHukiewitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a bit of work. Also, some tests checking the failure states (too big file, Content-Length mismatch) should be included.

src/aleph/web/controllers/storage.py Show resolved Hide resolved
src/aleph/web/controllers/storage.py Outdated Show resolved Hide resolved
src/aleph/web/controllers/storage.py Outdated Show resolved Hide resolved
Copy link
Member

@MHHukiewitz MHHukiewitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@MHHukiewitz MHHukiewitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After doing some research, I discovered that await request.post() reads the WHOLE request body into memory, clearly defeating the purpose of much of the code here.

Comment on lines +139 to +157

def read_file_with_max_size(self, max_size: int) -> Union[bytes, None]:
buffer_size = 64 * 1024 # 64 KB buffer size
content = b""
total_read = 0

while True:
chunk = self.file_field.file.read(buffer_size)

if not chunk:
break

total_read += len(chunk)
if total_read > max_size:
raise web.HTTPRequestEntityTooLarge()

content += chunk

return content if content else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is actually insufficient and in the actual position, where we process the request, we need to use

await request.multipart()

https://docs.aiohttp.org/en/stable/web_quickstart.html#file-uploads

You might have noticed a big warning in the example above. The general issue is that aiohttp.web.BaseRequest.post() reads the whole payload in memory, resulting in possible OOM errors. To avoid this, for multipart uploads, you should use aiohttp.web.BaseRequest.multipart() which returns a multipart reader:

@nesitor
Copy link
Member

nesitor commented Feb 29, 2024

Closed in favor #559

@nesitor nesitor closed this Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants