Skip to content

fix: prevent StreamedLog stop() from dropping tail or hanging on silent stream#754

Open
vdusek wants to merge 1 commit intomasterfrom
fix/streamed-log-stop-hang-and-tail-flush
Open

fix: prevent StreamedLog stop() from dropping tail or hanging on silent stream#754
vdusek wants to merge 1 commit intomasterfrom
fix/streamed-log-stop-hang-and-tail-flush

Conversation

@vdusek
Copy link
Copy Markdown
Contributor

@vdusek vdusek commented Apr 22, 2026

Summary

Two related bugs in StreamedLog / StreamedLogAsync (src/apify_client/_streamed_log.py):

  • Async — buffered tail was dropped on stop(). Shutdown is driven by stop() cancelling the streaming task. CancelledError unwinds out of async for before 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 in try/finally guarantees 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_logging flag 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 long iter_bytes() can block, so stop() unblocks within that window. The timeout is exposed as StreamedLog._read_timeout so 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.

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Apr 22, 2026
@vdusek vdusek self-assigned this Apr 22, 2026
@github-actions github-actions Bot added this to the 139th sprint - Tooling team milestone Apr 22, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Apr 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.32%. Comparing base (9ab096a) to head (5b6a9dd).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/_streamed_log.py 92.85% 1 Missing ⚠️
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     
Flag Coverage Δ
integration 95.32% <92.85%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…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>
@vdusek vdusek force-pushed the fix/streamed-log-stop-hang-and-tail-flush branch from ba41d95 to 5b6a9dd Compare April 22, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants