Skip to content

fix: close the platform events websocket iterator on shutdown - #1077

Merged
vdusek merged 5 commits into
masterfrom
fix/close-platform-events-websocket-iterator
Aug 4, 2026
Merged

fix: close the platform events websocket iterator on shutdown#1077
vdusek merged 5 commits into
masterfrom
fix/close-platform-events-websocket-iterator

Conversation

@vdusek

@vdusek vdusek commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Since v4.0.0, an Actor logs RuntimeError: aclose(): asynchronous generator is already running right after Actor.exit(). Cosmetic - the exit code stays 0 and no data is lost - but it shows up in every run and looks like a failure.

ApifyEventManager._process_platform_messages iterates websockets connect(...) with async for. That iterator is an async generator, and neither break nor cancelling the task closes it, so the garbage collector does - during asyncio.run teardown, where the aclose() it schedules lands after _cancel_all_tasks took its task snapshot and is never awaited. loop.shutdown_asyncgens() then calls aclose() on a generator that is still running. Up to v3.4.1 the code used async with connect(...), which unwound synchronously on cancellation.

contextlib.aclosing closes the iterator on every exit path - break, exception and cancellation alike. The rest of the diff is the loop body re-indenting, a unit test that asserts the iterator is closed, and an E2E test that asserts the error is absent from a run log.

Reported on Discord.

✍️ Drafted by Claude Code

@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 Aug 4, 2026
@vdusek vdusek self-assigned this Aug 4, 2026
@github-actions github-actions Bot added this to the 146th sprint - Tooling team milestone Aug 4, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Aug 4, 2026
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.90%. Comparing base (90d1725) to head (2d231f2).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/apify/events/_apify_event_manager.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1077      +/-   ##
==========================================
- Coverage   91.92%   91.90%   -0.03%     
==========================================
  Files          51       51              
  Lines        3232     3235       +3     
==========================================
+ Hits         2971     2973       +2     
- Misses        261      262       +1     
Flag Coverage Δ
e2e 35.30% <5.55%> (-0.04%) ⬇️
integration 57.03% <5.55%> (-0.09%) ⬇️
unit 83.27% <94.44%> (+0.01%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

@vdusek
vdusek requested a review from Mantisus August 4, 2026 11:51
@vdusek
vdusek marked this pull request as ready for review August 4, 2026 11:51

@Mantisus Mantisus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vdusek
vdusek requested a review from B4nan August 4, 2026 12:01
@vdusek
vdusek merged commit cad8d7e into master Aug 4, 2026
31 checks passed
@vdusek
vdusek deleted the fix/close-platform-events-websocket-iterator branch August 4, 2026 12:17
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.

4 participants