Skip to content

fix(events): prevent deadlock when closing or waiting for listeners from within a listener#2088

Open
vdusek wants to merge 4 commits into
masterfrom
fix/event-manager-listener-self-await
Open

fix(events): prevent deadlock when closing or waiting for listeners from within a listener#2088
vdusek wants to merge 4 commits into
masterfrom
fix/event-manager-listener-self-await

Conversation

@vdusek

@vdusek vdusek commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

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 the asyncio.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 a RecursionError.

The fix is two small changes in EventManager:

  • wait_for_all_listeners_to_complete() now excludes asyncio.current_task() from the gathered set, so a listener never waits on its own completion.
  • The listener wrapper's finally uses set.discard() instead of set.remove(), since __aexit__ may have already cleared the task set while the listener was mid-flight (avoids a spurious KeyError).

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. an ABORTING handler) - with this fix the SDK can drop its _detach_current_listener_task workaround.

✍️ Drafted by Claude Code

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

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.46%. Comparing base (2bc10ad) to head (c5405e2).

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     
Flag Coverage Δ
unit 93.46% <100.00%> (-0.02%) ⬇️

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.

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