fix: prevent duplicate platform websocket when nesting ApifyEventManager#1059
Open
vdusek wants to merge 3 commits into
Open
fix: prevent duplicate platform websocket when nesting ApifyEventManager#1059vdusek wants to merge 3 commits into
vdusek wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1059 +/- ##
==========================================
- Coverage 91.89% 91.84% -0.05%
==========================================
Files 51 51
Lines 3232 3239 +7
==========================================
+ Hits 2970 2975 +5
- Misses 262 264 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vdusek
marked this pull request as ready for review
July 21, 2026 12:56
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.
ApifyEventManageroverrode Crawlee'sEventManagerasync context manager without respecting the parent's ref counting. Crawlee ref-counts nestedasync withblocks, andBasicCrawler._run_crawleralways re-enters the same global event manager thatasync with Actor:already entered. Because the override started the platform websocket machinery on every enter and tore it down on every exit, the standardasync with Actor:+await crawler.run()pattern opened a second websocket connection.On the platform this meant every platform event (
MIGRATING,ABORTING,PERSIST_STATE) was delivered twice during a crawl, and on the inner exit the fields pointed at the second connection, so the first leaked and could never be cancelled. Each sequentialcrawler.run()added another connection; the platform caps at 10 per run and then closes with1008(a non-retryable code), permanently disabling platform events - so migration/abort handling silently stops for the rest of the run.The fix mirrors the parent's ref counting: the websocket machinery is started only on the outermost enter (
0 -> 1) and torn down only on the outermost exit (1 -> 0). Nested enters/exits reuse the single existing connection.Added two regression tests: a nested enter reuses the one connection and delivers each event exactly once; a nested exit leaves the outer context's connection working while the final exit tears everything down with no leaked task.
✍️ Drafted by Claude Code