From 968742ffbc3fcd8cbf7a16e05772d31a880527a6 Mon Sep 17 00:00:00 2001 From: Rudra Dudhat Date: Thu, 1 Oct 2026 02:40:49 +0530 Subject: [PATCH] fix(core): avoid redundant seek/read in LineIterator.__next__ line already holds the trailing remainder read a few lines above, no need to seek back and read the buffer again for the same bytes. Follow-up to #6279, per Claude's non-blocking review comment and rsareddy0329's suggestion to split it into a separate PR. --- sagemaker-core/src/sagemaker/core/iterators.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sagemaker-core/src/sagemaker/core/iterators.py b/sagemaker-core/src/sagemaker/core/iterators.py index 049a3a3cf5..04edf5a46a 100644 --- a/sagemaker-core/src/sagemaker/core/iterators.py +++ b/sagemaker-core/src/sagemaker/core/iterators.py @@ -179,12 +179,12 @@ def __next__(self): # Stream ended with a trailing partial line (no "\n"). # `continue` alone would spin forever here: byte_iterator # is already exhausted, so it keeps raising StopIteration - # and read_pos/buffer never change. Return the remainder - # once, so the next call correctly raises StopIteration. - self.buffer.seek(self.read_pos) - remainder = self.buffer.read() - self.read_pos += len(remainder) - return remainder + # and read_pos/buffer never change. `line` (read above) + # already holds this same remainder, since nothing moves + # the buffer between that read and here. Return it once, + # so the next call correctly raises StopIteration. + self.read_pos += len(line) + return line raise if "PayloadPart" not in chunk: # handle API response errors and force terminate.