Skip to content

Fix LineIterator hanging forever when the stream ends without a trailing newline - #6279

Open
RudraDudhat2509 wants to merge 1 commit into
aws:masterfrom
RudraDudhat2509:fix/lineiterator-no-trailing-newline-hang
Open

RudraDudhat2509 wants to merge 1 commit into
aws:masterfrom
RudraDudhat2509:fix/lineiterator-no-trailing-newline-hang

Conversation

@RudraDudhat2509

Copy link
Copy Markdown

Fixes #6278

Returns the leftover partial line once instead of continue, so the next call correctly hits read_pos == buffer size and raises StopIteration normally instead of looping forever.

Added test_line_iterator_no_trailing_newline_at_end. test_line_iterator_incomplete_line_at_end doesn't actually cover this, it only tests a stream that already ends with \n.

Verified against the pre-fix code: hangs, confirmed by killing it after 15s with no output. Full test_iterators.py 21 passed after the fix.

…ing newline

When the underlying event stream is exhausted (StopIteration) while the
buffer still has an unread trailing partial line (no "\n" at the end,
a normal case since the model's last streamed token has no reason to
be a newline), __next__ just did `continue`. That re-reads the exact
same unterminated line, fails the same check, and calls next() on an
already-exhausted iterator again, forever. Nothing in that branch ever
changes state, so it spins indefinitely instead of returning or
raising.

Return the leftover partial data once instead, so the following call
correctly hits the read_pos == buffer size case and raises
StopIteration normally.

Added test_line_iterator_no_trailing_newline_at_end since the existing
test_line_iterator_incomplete_line_at_end doesn't actually cover this,
it only tests a stream that ends cleanly with a trailing newline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant