fix(events): prevent deadlock when closing or waiting for listeners from within a listener#2088
Open
vdusek wants to merge 4 commits into
Open
fix(events): prevent deadlock when closing or waiting for listeners from within a listener#2088vdusek wants to merge 4 commits into
vdusek wants to merge 4 commits into
Conversation
…rom within a listener
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2088 +/- ##
==========================================
- Coverage 93.47% 93.46% -0.02%
==========================================
Files 181 181
Lines 12552 12560 +8
==========================================
+ Hits 11733 11739 +6
- Misses 819 821 +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:
|
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.
Calling
EventManager.wait_for_all_listeners_to_complete()- or closing the manager via__aexit__- from within an event listener deadlocked. The listener runs in a task that is itself registered in_listener_tasks, so theasyncio.gather(...)inside the wait ends up awaiting the very task that is awaiting it. Under a close timeout this self-await cycle degrades further into aRecursionError.The fix is two small changes in
EventManager:wait_for_all_listeners_to_complete()now excludesasyncio.current_task()from the gathered set, so a listener never waits on its own completion.finallyusesset.discard()instead ofset.remove(), since__aexit__may have already cleared the task set while the listener was mid-flight (avoids a spuriousKeyError).Both are no-ops for the normal case (a non-listener caller waiting for listeners). Added regression tests covering waiting for all listeners from within a listener, and closing the manager from within a listener.
This unblocks apify/apify-sdk-python#1061, where
Actor.exit()is called from inside an event listener (e.g. anABORTINGhandler) - with this fix the SDK can drop its_detach_current_listener_taskworkaround.✍️ Drafted by Claude Code