fix(events): open the platform websocket only once per context - #1082
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1082 +/- ##
==========================================
+ Coverage 92.20% 92.23% +0.03%
==========================================
Files 51 51
Lines 3231 3247 +16
==========================================
+ Hits 2979 2995 +16
Misses 252 252
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
August 6, 2026 11:33
B4nan
approved these changes
Aug 6, 2026
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.
ApifyEventManagerignored the re-entrancy contract of Crawlee'sEventManager, which tracks active contexts with_active_ref_count.BasicCrawler._run_crawleralways enters the global event manager, andActor.initregisters theApifyEventManageras exactly that and has already entered it — so on the platform every crawler run re-entered it and opened a second websocket.Measured on master, with a local events server and a
BasicCrawler.run()inside an enteredApifyEventManager:Migratingbecame twoMigratinglistener calls and twoPersistState(is_migrating=True)— state persisted twice per migration. The extra connection also counts against the platform limit of 10 per run.__aexit__bookkeeping pointing at an already-closed one. The Actor's own connection and its message-processing task therefore surviveActor.exit(), re-breaking the iterator shutdown from fix: close the platform events websocket iterator on shutdown #1077.The websocket is now owned by the outermost context only, mirroring
LocalEventManager. On top of that:_teardown_platform_websocket()and the parent context is left in afinally, so a failed shutdown can no longer keep the manager active for good — which would mean never emittingPersistStateagain.__cause__of theRuntimeError, not only logged.Actor.reboot()reads_listeners_to_wrapperswithget, so the lookup no longer inserts entries for events nobody listens to.Adopts the patterns from apify/crawlee-python#2100 (Crawlee 1.9.1).
✍️ Drafted by Claude Code