Skip to content

LineIterator hangs forever if the streamed response ends without a trailing newline #6278

Description

@RudraDudhat2509

LineIterator.__next__ hangs forever if the last chunk of a streamed response has no trailing newline.

sagemaker-core/src/sagemaker/core/iterators.py:

except StopIteration:
    if self.read_pos < self.buffer.getbuffer().nbytes:
        continue
    raise

continue re-reads the same unterminated line, fails the same check, calls next() on the now-exhausted byte_iterator again, gets StopIteration again, forever. Nothing in that branch changes read_pos or the buffer.

Repro:

from sagemaker.core.iterators import LineIterator

chunks = [
    {"PayloadPart": {"Bytes": b'{"outputs": [" a"]}\n'}},
    {"PayloadPart": {"Bytes": b'{"outputs": [" b"]}'}},
]

for line in LineIterator(iter(chunks)):
    print(line)

Prints the first line then hangs.

Will open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions