fix(events): correct the listener invocation, removal, and context release - #2100
Draft
vdusek wants to merge 13 commits into
Draft
fix(events): correct the listener invocation, removal, and context release#2100vdusek wants to merge 13 commits into
vdusek wants to merge 13 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## fix/event-manager-listener-self-await #2100 +/- ##
=========================================================================
- Coverage 93.64% 93.62% -0.02%
=========================================================================
Files 181 181
Lines 12595 12605 +10
=========================================================================
+ Hits 11794 11801 +7
- Misses 801 804 +3
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.
Stacked on top of #2088 - review that one first.
A polish pass over
EventManagerandLocalEventManager. No public API changes.Fixes
__call__is async (a class instance, not a function) was treated as sync and handed toasyncio.to_thread, which returned its coroutine without ever awaiting it - the listener silently never ran.off()decided between "remove this listener" and "remove all listeners of the event" by the truthiness oflistener, so a listener object that is falsy in a boolean context wiped all listeners of the event.off()left an empty entry behind in_listeners_to_wrappers, keeping a reference to a listener that is no longer registered, and created entries for events and listeners that were never registered at all.__aexit__released the context only on its happy path. A cancellation or a failing emission left the manager active for good, and since re-entering an active manager is a no-op,PersistStatewas never emitted again.pyee is gone
emitcreates the listener task itself instead of going throughpyee.asyncio.AsyncIOEventEmitter, which:emit, sowait_for_all_listeners_to_complete()can no longer miss the listeners of a just-emitted event - the one-tick defer that fix(events): prevent deadlock when closing or waiting for listeners from within a listener #2088 had to make explicit is not needed anymore,pyeeis still pulled in byplaywrightfor the browser extras).The
_listeners_to_wrapperslayout is deliberately kept as it is - apify-sdk-python reaches into it inActor.reboot(). Its full unit test suite passes against this branch.The rest
on()into_wrap_listener.__aexit__branches were collapsed into a singlelast_exitflag,__aenter__mirrors it, and the manual "not active" check was replaced by theensure_contextdecorator, so the message matches the one every other method raises.EventManager.on.listener_wrapper(): ...DEBUG lines are gone - they reported that a task is awaited, that it completed, and that it was discarded.LocalEventManagerreads the CPU and the memory info concurrently -get_cpu_infoalone blocks its thread for 100 ms while sampling the CPU.__aenter__ -> Self,set[asyncio.Task[None]], and theemitimplementation is annotated as widely as its last overload.wait_for_all_tasks_for_finishrenamed towait_for_all_tasks_to_finish.EventManager, and the attributes of both managers are documented with docstrings instead of comments.New tests cover every fix above, the synchronous task registration in
emit, the nested context teardown, and the concurrent system info readings.✍️ Drafted by Claude Code