Repository navigation
fix: run sync handlers via anyio.to_thread.run_sync for cancellation #33
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
Closed
neubig
wants to merge
3
commits into
browser-use:main
from
neubig:fix/anyio-cancelable-sync-handlers
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Test that sync handler timeout doesn't hang the event bus. | ||
|
|
||
| Option A: sync handlers now run via anyio.to_thread.run_sync with | ||
| abandon_on_cancel=True, so the timeout can cancel the await and the bus | ||
| stays responsive. The worker thread may still be alive (Python can't kill | ||
| threads), but the bus is no longer blocked on it. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import threading | ||
|
|
||
| import pytest | ||
|
|
||
| from bubus import BaseEvent, EventBus | ||
|
|
||
|
|
||
| class BlockEvent(BaseEvent[str]): | ||
| pass | ||
|
|
||
|
|
||
| class FastEvent(BaseEvent[str]): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_sync_handler_timeout_does_not_block_subsequent_events(): | ||
| """A sync handler that blocks must not prevent subsequent events from | ||
| being processed after the timeout fires. | ||
|
|
||
| Before the fix: sync handlers ran directly in the event loop thread, | ||
| so a blocking sync handler blocked the entire loop — the timeout never | ||
| fired and no other events could be processed. | ||
|
|
||
| After the fix: sync handlers run via anyio.to_thread.run_sync, so the | ||
| event loop stays free to enforce the timeout and process other events. | ||
| """ | ||
| thread_started = threading.Event() | ||
| thread_should_stop = threading.Event() | ||
| fast_event_handled = threading.Event() | ||
|
|
||
| def blocking_sync_handler(event: BlockEvent) -> str: | ||
| thread_started.set() | ||
| thread_should_stop.wait(timeout=30) | ||
|
neubig marked this conversation as resolved.
|
||
| return "done" | ||
|
|
||
| def fast_sync_handler(event: FastEvent) -> str: | ||
| fast_event_handled.set() | ||
| return "fast" | ||
|
|
||
| bus = EventBus(name="test_sync_timeout") | ||
| bus.on(BlockEvent, blocking_sync_handler) | ||
| bus.on(FastEvent, fast_sync_handler) | ||
| bus._start() | ||
|
|
||
| # Dispatch the blocking event | ||
| bus.dispatch(BlockEvent(event_timeout=1.0)) | ||
| try: | ||
| await asyncio.wait_for(bus.step(timeout=1.0), timeout=5.0) | ||
| except (asyncio.TimeoutError, TimeoutError, Exception): | ||
|
neubig marked this conversation as resolved.
|
||
| pass # Expected — the handler timed out | ||
|
|
||
| # Now dispatch a fast event — it must be processed quickly. | ||
| # Before the fix, the event loop was blocked by the sync handler | ||
| # and this would hang. | ||
| bus.dispatch(FastEvent()) | ||
| await asyncio.wait_for(bus.step(timeout=1.0), timeout=5.0) | ||
|
|
||
| thread_should_stop.set() | ||
|
neubig marked this conversation as resolved.
|
||
| await bus.stop(timeout=1, clear=True) | ||
|
|
||
| assert thread_started.is_set(), "Blocking handler should have started" | ||
| assert fast_event_handled.is_set(), "Fast event should have been processed" | ||
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.