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(service): file upload memory leak #3179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions renku/ui/service/controllers/cache_files_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from renku.ui.service.controllers.api.abstract import ServiceCtrl
from renku.ui.service.controllers.api.mixins import RenkuOperationMixin
from renku.ui.service.errors import IntermittentFileExistsError, UserUploadTooLargeError
from renku.ui.service.serializers.cache import FileUploadRequest, FileUploadResponseRPC, extract_file
from renku.ui.service.serializers.cache import FileUploadRequest, FileUploadResponseRPC, extract_filename, extract_file
from renku.ui.service.views import result_response


Expand All @@ -41,14 +41,15 @@ class UploadFilesCtrl(ServiceCtrl, RenkuOperationMixin):

def __init__(self, cache, user_data, flask_request):
"""Construct controller."""
self.file = extract_file(flask_request)
self.filename = extract_filename(flask_request)
file = extract_file(flask_request)

self.response_builder = {
"file_name": self.file.filename,
"content_type": self.file.content_type,
"is_archive": self.file.content_type in SUPPORTED_ARCHIVES,
"file_name": self.filename,
"content_type": file.content_type,
"is_archive": file.content_type in SUPPORTED_ARCHIVES,
}
args = {**flask_request.args, **flask_request.form}
args = {**flask_request.args}
self.response_builder.update(UploadFilesCtrl.REQUEST_SERIALIZER.load(args))

super(UploadFilesCtrl, self).__init__(cache, user_data, {})
Expand All @@ -66,14 +67,14 @@ def user_cache_dir(self) -> Path:

return directory

def process_upload(self):
def process_upload(self, file):
"""Process an upload."""
if self.response_builder.get("chunked_id", None) is None:
return self.process_file()
return self.process_file(file)

return self.process_chunked_upload()
return self.process_chunked_upload(file)

def process_chunked_upload(self):
def process_chunked_upload(self, file):
"""Process upload done in chunks."""
if self.response_builder["total_size"] > MAX_CONTENT_LENGTH:
if MAX_CONTENT_LENGTH > 524288000:
Expand All @@ -95,7 +96,7 @@ def process_chunked_upload(self):
file_path = chunks_dir / str(current_chunk)
relative_path = file_path.relative_to(chunks_dir)

self.file.save(str(file_path))
file.save(str(file_path))

with self.cache.model_db.lock(f"chunked_upload_{self.user.user_id}_{chunked_id}"):
self.cache.set_file_chunk(
Expand All @@ -111,13 +112,13 @@ def process_chunked_upload(self):
if not completed:
return {}

target_file_path = self.user_cache_dir / self.file.filename
target_file_path = self.user_cache_dir / self.filename

if target_file_path.exists():
if self.response_builder.get("override_existing", False):
target_file_path.unlink()
else:
raise IntermittentFileExistsError(file_name=self.file.filename)
raise IntermittentFileExistsError(file_name=self.filename)

with open(target_file_path, "wb") as target_file:
for file_number in range(total_chunks):
Expand All @@ -131,17 +132,17 @@ def process_chunked_upload(self):

return self.postprocess_file(target_file_path)

def process_file(self):
def process_file(self, file):
"""Process uploaded file."""

file_path = self.user_cache_dir / self.file.filename
file_path = self.user_cache_dir / self.filename
if file_path.exists():
if self.response_builder.get("override_existing", False):
file_path.unlink()
else:
raise IntermittentFileExistsError(file_name=self.file.filename)
raise IntermittentFileExistsError(file_name=self.filename)

self.file.save(str(file_path))
file.save(str(file_path))

return self.postprocess_file(file_path)

Expand Down Expand Up @@ -193,6 +194,7 @@ def renku_op(self):
# NOTE: We leave it empty since it does not execute renku operation.
pass

def to_response(self):
def to_response(self, flask_request):
"""Execute controller flow and serialize to service response."""
return result_response(UploadFilesCtrl.RESPONSE_SERIALIZER, self.process_upload())
file = extract_file(flask_request)
return result_response(UploadFilesCtrl.RESPONSE_SERIALIZER, self.process_upload(file))
11 changes: 9 additions & 2 deletions renku/ui/service/serializers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
)
from renku.ui.service.serializers.rpc import JsonRPCResponse


def extract_file(request):
"""Extract file from Flask request.

Expand All @@ -53,9 +52,17 @@ def extract_file(request):
raise ValidationError("wrong filename: {0}".format(file.filename))

if file:
file.filename = secure_filename(file.filename)
return file

def extract_filename(request):
"""Extract filename from Flask request.

Raises:
`ValidationError`: If file data or filename is missing in request.
"""
file = extract_file(request)
return secure_filename(file.filename)


class FileUploadRequest(ArchiveSchema):
"""Request schema for file upload."""
Expand Down
2 changes: 1 addition & 1 deletion renku/ui/service/views/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def upload_file_view(user_data, cache):
tags:
- cache
"""
return UploadFilesCtrl(cache, user_data, request).to_response()
return UploadFilesCtrl(cache, user_data, request).to_response(request)


@cache_blueprint.route(
Expand Down