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

Open Beginner friendly
#6,278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api, backend

Research direction

Start in sagemaker-core/src/sagemaker/core/iterators.py at LineIterator.next, then run the provided reproduction with a final streamed chunk that lacks a trailing newline. Confirm that iteration yields both response lines and terminates instead of retrying the exhausted byte iterator forever; add coverage for this unterminated final chunk if the surrounding tests provide a suitable location.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
2.3k
Forks
1.3k
Avg merge
3d 9h
Merged PRs (30d)
42

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aws/sagemaker-python-sdk

All issues in aws/sagemaker-python-sdk

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.