Repository navigation
fix: prevent stopped event buses from restarting a busy loop #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zwk110
wants to merge
3
commits into
browser-use:main
Choose a base branch
from
zwk110:codex/stop-shutdown-eventbus-loops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # pyright: basic | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from bubus import BaseEvent, EventBus | ||
| from bubus.service import QueueShutDown | ||
|
|
||
|
|
||
| class PingEvent(BaseEvent): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('clear', [False, True]) | ||
| async def test_late_dispatch_does_not_restart_stopped_bus(clear: bool): | ||
| bus = EventBus() | ||
| await bus.dispatch(PingEvent()) | ||
| await bus.stop(timeout=1, clear=clear) | ||
|
|
||
| try: | ||
| with pytest.raises(QueueShutDown): | ||
| bus.dispatch(PingEvent()) | ||
| assert not bus._is_running | ||
| assert bus._runloop_task is None | ||
| finally: | ||
| await bus.stop() | ||
|
|
||
|
|
||
| async def test_cancel_idle_runloop_closes_queue_waiter(): | ||
| bus = EventBus() | ||
| await bus.dispatch(PingEvent()) | ||
| await bus.wait_until_idle() | ||
| task = bus._runloop_task | ||
| assert task is not None | ||
| assert bus.event_queue is not None | ||
|
|
||
| # Cancel while the loop is waiting for its next event. | ||
| async with asyncio.timeout(1): | ||
| while not bus.event_queue._getters: # noqa: ASYNC110 - wait for the real queue waiter before cancellation | ||
| await asyncio.sleep(0) | ||
|
|
||
| try: | ||
| task.cancel() | ||
| done, _ = await asyncio.wait({task}, timeout=1) | ||
| assert task in done | ||
| assert not bus._is_running | ||
| assert not bus.event_queue._getters | ||
| finally: | ||
| await bus.stop() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('clear', [False, True]) | ||
| async def test_wait_until_idle_after_stop_does_not_restart_bus(clear: bool): | ||
| bus = EventBus() | ||
| await bus.dispatch(PingEvent()) | ||
| await bus.stop(timeout=1, clear=clear) | ||
|
|
||
| await bus.wait_until_idle(timeout=1) | ||
|
|
||
| assert not bus._is_running | ||
| assert bus._runloop_task is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('clear', [False, True]) | ||
| async def test_stop_releases_pending_idle_wait(clear: bool): | ||
| bus = EventBus() | ||
| started, release = asyncio.Event(), asyncio.Event() | ||
|
|
||
| async def handle(event: PingEvent): | ||
| started.set() | ||
| await release.wait() | ||
|
|
||
| bus.on(PingEvent, handle) | ||
| bus.dispatch(PingEvent()) | ||
| bus.dispatch(PingEvent()) | ||
| await asyncio.wait_for(started.wait(), timeout=1) | ||
| waiter = asyncio.create_task(bus.wait_until_idle()) | ||
| await asyncio.sleep(0) | ||
| assert not waiter.done() | ||
|
|
||
| try: | ||
| await bus.stop(timeout=0, clear=clear) | ||
| done, _ = await asyncio.wait({waiter}, timeout=1) | ||
| assert waiter in done | ||
| assert waiter.result() is None | ||
| assert not bus._is_running | ||
| finally: | ||
| release.set() | ||
| waiter.cancel() | ||
| await asyncio.gather(waiter, return_exceptions=True) | ||
| await bus.stop() | ||
|
|
||
|
|
||
| async def test_idle_wait_after_new_dispatch_waits_for_handler(): | ||
| bus = EventBus() | ||
| await bus.wait_until_idle() | ||
| started, release = asyncio.Event(), asyncio.Event() | ||
|
|
||
| async def handle(event: PingEvent): | ||
| started.set() | ||
| await release.wait() | ||
|
|
||
| bus.on(PingEvent, handle) | ||
| event = bus.dispatch(PingEvent()) | ||
| waiter = asyncio.create_task(bus.wait_until_idle()) | ||
| try: | ||
| await asyncio.wait_for(started.wait(), timeout=1) | ||
| assert not waiter.done() | ||
| release.set() | ||
| await asyncio.wait_for(waiter, timeout=1) | ||
| assert event.event_status == 'completed' | ||
| assert bus.event_queue is not None | ||
| assert bus.event_queue.empty() | ||
| finally: | ||
| release.set() | ||
| waiter.cancel() | ||
| await asyncio.gather(waiter, return_exceptions=True) | ||
| await bus.stop() | ||
|
|
||
|
|
||
| async def test_queue_shutdown_exits_runloop(): | ||
| bus = EventBus() | ||
| await bus.dispatch(PingEvent()) | ||
| await bus.wait_until_idle() | ||
| task = bus._runloop_task | ||
| assert task is not None | ||
| assert bus.event_queue is not None | ||
|
|
||
| async with asyncio.timeout(1): | ||
| while not bus.event_queue._getters: # noqa: ASYNC110 - wait for the real queue waiter before shutdown | ||
| await asyncio.sleep(0) | ||
|
|
||
| try: | ||
| bus.event_queue.shutdown() | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| done, _ = await asyncio.wait({task}, timeout=1) | ||
| assert task in done | ||
| assert not bus._is_running | ||
| assert not bus.event_queue._getters | ||
| finally: | ||
| await bus.stop() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.