fix: prevent StreamedLog stop() from dropping tail or hanging on silent stream#754
Open
fix: prevent StreamedLog stop() from dropping tail or hanging on silent stream#754
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #754 +/- ##
==========================================
+ Coverage 95.22% 95.32% +0.09%
==========================================
Files 45 45
Lines 5154 5156 +2
==========================================
+ Hits 4908 4915 +7
+ Misses 246 241 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…nt stream The async variant lost the final buffered log line because the trailing `_log_buffer_content(include_last_part=True)` was skipped when `stop()` cancelled the task; wrapping the loop in try/finally ensures the tail is always flushed. The sync variant could hang in `iter_bytes()` on a silent stream — pass a 30s read timeout (overridable via the new `_read_timeout` class attribute) so `stop()` can unblock within bounded time. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ba41d95 to
5b6a9dd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related bugs in
StreamedLog/StreamedLogAsync(src/apify_client/_streamed_log.py):Async — buffered tail was dropped on
stop(). Shutdown is driven bystop()cancelling the streaming task.CancelledErrorunwinds out ofasync forbefore the trailing_log_buffer_content(include_last_part=True)runs, so whatever sits in the buffer (the tail after the last 8601 marker) is silently discarded. Wrapping the loop intry/finallyguarantees the tail is flushed even on cancel.Sync —
stop()could hang on a silent stream.iter_bytes()blocks on the socket read, so the_stop_loggingflag is never observed until the next chunk arrives or the long-polling timeout (360s) elapses. Passing a 30s read timeout to_log_client.stream()caps how longiter_bytes()can block, sostop()unblocks within that window. The timeout is exposed asStreamedLog._read_timeoutso it can be tuned (or shortened in tests) without monkeypatching a module global.Tradeoff: the sync stream now ends if the actor goes silent for more than 30s (vs. the prior 360s). This is documented on the class attribute and is the intended cost of bounded
stop().Two regression tests cover both bugs.