From 69c20dfebf6ba4c7091ff33e0979dbe15c6cab78 Mon Sep 17 00:00:00 2001 From: Wallysson Silva Date: Tue, 22 Oct 2024 16:41:04 -0300 Subject: [PATCH] Fix high memory usage is USM client when uploading a file This commit ensures that software client will not load the entire ISO file into memory, avoids memory spikes. Test Plan: PASS - software --os-region-name SystemController upload files without memory spikes PASS - software upload --local without memory spikes Closes-Bug: 2085380 Change-Id: If84d6b78269d6bc2be4e325dacc4be9556576c2a Signed-off-by: Wallysson Silva --- software-client/software_client/v1/release.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/software-client/software_client/v1/release.py b/software-client/software_client/v1/release.py index 52df1fc8..1c594531 100644 --- a/software-client/software_client/v1/release.py +++ b/software-client/software_client/v1/release.py @@ -4,6 +4,7 @@ # SPDX-License-Identifier: Apache-2.0 # +import contextlib import os import re import signal @@ -105,14 +106,14 @@ class ReleaseManager(base.Manager): print("No file to be uploaded.") return 1 - for software_file in valid_files: - with open(software_file, 'rb') as file: - data_content = file.read() - to_upload_files[software_file] = (software_file, data_content) + with contextlib.ExitStack() as stack: + for software_file in valid_files: + file = stack.enter_context(open(software_file, 'rb')) + to_upload_files[software_file] = (software_file, file) - encoder = MultipartEncoder(fields=to_upload_files) - headers = {'Content-Type': encoder.content_type} - return self._create_multipart(path, body=encoder, headers=headers) + encoder = MultipartEncoder(fields=to_upload_files) + headers = {'Content-Type': encoder.content_type} + return self._create_multipart(path, body=encoder, headers=headers) def upload_dir(self, args): # arg.release is a list