Skip to content

Commit 731cb97

Browse files
committed
Fix memory leak issue
1 parent 1e7c987 commit 731cb97

1 file changed

Lines changed: 77 additions & 58 deletions

File tree

tosfs/core.py

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,29 +2469,35 @@ def _upload_chunk(self, final: bool = False) -> bool:
24692469
self.buffer = io.BytesIO()
24702470

24712471
self.buffer.seek(0)
2472-
content = self.buffer.read()
2473-
part = retryable_func_executor(
2474-
lambda: self.fs.tos_client.upload_part(
2475-
self.bucket,
2476-
self.key,
2477-
self.upload_id,
2478-
self.part_number,
2479-
content=content,
2480-
),
2481-
max_retry_num=self.fs.max_retry_num,
2482-
)
2483-
self.parts.append(
2484-
PartInfo(
2485-
part_number=self.part_number,
2486-
etag=part.etag,
2487-
part_size=len(content),
2488-
offset=None,
2489-
hash_crc64_ecma=None,
2490-
is_completed=None,
2472+
content = None
2473+
try:
2474+
content = self.buffer.read()
2475+
part = retryable_func_executor(
2476+
lambda: self.fs.tos_client.upload_part(
2477+
self.bucket,
2478+
self.key,
2479+
self.upload_id,
2480+
self.part_number,
2481+
content=content,
2482+
),
2483+
max_retry_num=self.fs.max_retry_num,
24912484
)
2492-
)
2493-
self.part_number += 1
2494-
self.buffer = io.BytesIO()
2485+
self.parts.append(
2486+
PartInfo(
2487+
part_number=self.part_number,
2488+
etag=part.etag,
2489+
part_size=len(content),
2490+
offset=None,
2491+
hash_crc64_ecma=None,
2492+
is_completed=None,
2493+
)
2494+
)
2495+
self.part_number += 1
2496+
self.buffer = io.BytesIO()
2497+
finally:
2498+
content = None
2499+
self.buffer.seek(0)
2500+
self.buffer.truncate()
24952501

24962502
if self.autocommit and final:
24972503
self.commit()
@@ -2543,47 +2549,60 @@ def fetch() -> bytes:
25432549
def commit(self) -> None:
25442550
"""Complete multipart upload or PUT."""
25452551
logger.debug("Commit %s", self)
2546-
if self.tell() == 0 and self.upload_id is not None:
2547-
if self.buffer is not None:
2548-
logger.debug("Empty file committed %s", self)
2552+
data = None
2553+
try:
2554+
if self.tell() == 0 and self.upload_id is not None:
2555+
if self.buffer is not None:
2556+
logger.debug("Empty file committed %s", self)
2557+
retryable_func_executor(
2558+
lambda: self.fs.tos_client.abort_multipart_upload(
2559+
self.bucket, self.key, self.upload_id
2560+
),
2561+
max_retry_num=self.fs.max_retry_num,
2562+
)
2563+
self.fs.touch(self.path, **self.kwargs)
2564+
elif self.upload_id is None and self.buffer is not None:
2565+
logger.debug("One-shot upload of %s", self)
2566+
self.buffer.seek(0)
2567+
data = self.buffer.read()
25492568
retryable_func_executor(
2550-
lambda: self.fs.tos_client.abort_multipart_upload(
2551-
self.bucket, self.key, self.upload_id
2569+
lambda: self.fs.tos_client.put_object(
2570+
self.bucket, self.key, content=data
2571+
),
2572+
max_retry_num=self.fs.max_retry_num,
2573+
)
2574+
elif self.upload_id is not None:
2575+
logger.debug("Complete multi-part upload for %s ", self)
2576+
retryable_func_executor(
2577+
lambda: self.fs.tos_client.complete_multipart_upload(
2578+
self.bucket,
2579+
self.key,
2580+
upload_id=self.upload_id,
2581+
parts=self.parts,
25522582
),
25532583
max_retry_num=self.fs.max_retry_num,
25542584
)
2555-
self.fs.touch(self.path, **self.kwargs)
2556-
elif self.upload_id is None and self.buffer is not None:
2557-
logger.debug("One-shot upload of %s", self)
2558-
self.buffer.seek(0)
2559-
data = self.buffer.read()
2560-
retryable_func_executor(
2561-
lambda: self.fs.tos_client.put_object(
2562-
self.bucket, self.key, content=data
2563-
),
2564-
max_retry_num=self.fs.max_retry_num,
2565-
)
2566-
elif self.upload_id is not None:
2567-
logger.debug("Complete multi-part upload for %s ", self)
2568-
retryable_func_executor(
2569-
lambda: self.fs.tos_client.complete_multipart_upload(
2570-
self.bucket,
2571-
self.key,
2572-
upload_id=self.upload_id,
2573-
parts=self.parts,
2574-
),
2575-
max_retry_num=self.fs.max_retry_num,
2576-
)
25772585

2578-
self.buffer = None
2586+
finally:
2587+
self.closed = True
2588+
self.buffer = None
2589+
self.upload_id = None
2590+
self.parts = []
2591+
data = None
25792592

25802593
def discard(self) -> None:
25812594
"""Close the file without writing."""
2582-
if self.upload_id:
2583-
retryable_func_executor(
2584-
lambda: self.fs.tos_client.abort_multipart_upload(
2585-
self.bucket, self.key, self.upload_id
2586-
),
2587-
max_retry_num=self.fs.max_retry_num,
2588-
)
2589-
self.buffer = None
2595+
try:
2596+
if self.upload_id:
2597+
retryable_func_executor(
2598+
lambda: self.fs.tos_client.abort_multipart_upload(
2599+
self.bucket, self.key, self.upload_id
2600+
),
2601+
max_retry_num=self.fs.max_retry_num,
2602+
)
2603+
self.buffer = None
2604+
finally:
2605+
self.closed = True
2606+
self.buffer = None
2607+
self.upload_id = None
2608+
self.parts = []

0 commit comments

Comments
 (0)