-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: add timeout protection for event hook calls #8431
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
leafliber
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
leafliber:fix/hook-timeout
base: master
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 1 commit
Commits
Show all changes
2 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,219 @@ | ||
| """Tests for call_event_hook timeout protection.""" | ||
|
|
||
| import asyncio | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from astrbot.core.pipeline.context_utils import call_event_hook | ||
| from astrbot.core.star.star_handler import EventType | ||
|
|
||
|
|
||
| def _make_handler_metadata( | ||
| handler_coro, module_path="test_module", handler_name="test_handler" | ||
| ): | ||
| handler = MagicMock() | ||
| handler.handler_module_path = module_path | ||
| handler.handler_name = handler_name | ||
| handler.handler = handler_coro | ||
| handler.enabled = True | ||
| return handler | ||
|
|
||
|
|
||
| def _make_event(stopped=False, plugins_name=None): | ||
| event = MagicMock() | ||
| event.unified_msg_origin = "test_umo" | ||
| event.plugins_name = plugins_name or [] | ||
| event.is_stopped = MagicMock(return_value=stopped) | ||
| return event | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_star_map(): | ||
| with patch("astrbot.core.pipeline.context_utils.star_map") as sm: | ||
| sm.__getitem__ = MagicMock(return_value=MagicMock(name="TestPlugin")) | ||
| yield sm | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_handlers_registry(): | ||
| with patch( | ||
| "astrbot.core.pipeline.context_utils.star_handlers_registry" | ||
| ) as registry: | ||
| yield registry | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_completes_within_timeout(mock_star_map, mock_handlers_registry): | ||
| handler_fn = AsyncMock() | ||
| handler_md = _make_handler_metadata(handler_fn) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=5.0) | ||
|
|
||
| handler_fn.assert_awaited_once() | ||
| assert result is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_timeout_skips_handler(mock_star_map, mock_handlers_registry): | ||
| async def slow_handler(*args, **kwargs): | ||
| await asyncio.sleep(10) | ||
|
|
||
| handler_md = _make_handler_metadata(slow_handler) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=0.5) | ||
|
|
||
| assert result is False | ||
|
|
||
|
|
||
|
leafliber marked this conversation as resolved.
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_timeout_does_not_block_subsequent_handlers( | ||
| mock_star_map, mock_handlers_registry | ||
| ): | ||
| async def slow_handler(*args, **kwargs): | ||
| await asyncio.sleep(10) | ||
|
|
||
| fast_handler_fn = AsyncMock() | ||
| slow_md = _make_handler_metadata( | ||
| slow_handler, module_path="slow_mod", handler_name="slow_h" | ||
| ) | ||
| fast_md = _make_handler_metadata( | ||
| fast_handler_fn, module_path="fast_mod", handler_name="fast_h" | ||
| ) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[slow_md, fast_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=0.5) | ||
|
|
||
| fast_handler_fn.assert_awaited_once() | ||
| assert result is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_timeout_zero_disables_timeout( | ||
| mock_star_map, mock_handlers_registry | ||
| ): | ||
| async def slow_handler(*args, **kwargs): | ||
| await asyncio.sleep(0.3) | ||
|
|
||
| handler_md = _make_handler_metadata(slow_handler) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=0) | ||
|
|
||
| assert result is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_timeout_negative_disables_timeout( | ||
| mock_star_map, mock_handlers_registry | ||
| ): | ||
| async def slow_handler(*args, **kwargs): | ||
| await asyncio.sleep(0.3) | ||
|
|
||
| handler_md = _make_handler_metadata(slow_handler) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=-1) | ||
|
|
||
| assert result is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_exception_continues(mock_star_map, mock_handlers_registry): | ||
| async def failing_handler(*args, **kwargs): | ||
| raise RuntimeError("test error") | ||
|
|
||
| handler_md = _make_handler_metadata(failing_handler) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent) | ||
|
|
||
| assert result is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_hook_stops_event_propagation(mock_star_map, mock_handlers_registry): | ||
| handler_fn = AsyncMock() | ||
| handler_md = _make_handler_metadata(handler_fn) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event(stopped=True) | ||
|
|
||
| result = await call_event_hook(event, EventType.OnLLMRequestEvent) | ||
|
|
||
| assert result is True | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_default_timeout_value(mock_star_map, mock_handlers_registry): | ||
| import inspect | ||
|
|
||
| sig = inspect.signature(call_event_hook) | ||
| timeout_param = sig.parameters["timeout"] | ||
| assert timeout_param.default == 300.0 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_timeout_logs_plugin_name(mock_star_map, mock_handlers_registry): | ||
| async def slow_handler(*args, **kwargs): | ||
| await asyncio.sleep(10) | ||
|
|
||
| handler_md = _make_handler_metadata( | ||
| slow_handler, module_path="my_plugin_module", handler_name="on_llm_req" | ||
| ) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| with patch("astrbot.core.pipeline.context_utils.logger") as mock_logger: | ||
| await call_event_hook(event, EventType.OnLLMRequestEvent, timeout=0.2) | ||
|
|
||
| warning_calls = [ | ||
| call for call in mock_logger.warning.call_args_list if "timed out" in str(call) | ||
| ] | ||
| assert len(warning_calls) == 1 | ||
| warning_msg = str(warning_calls[0]) | ||
| assert "on_llm_req" in warning_msg | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_args_kwargs_passed_to_handler(mock_star_map, mock_handlers_registry): | ||
| handler_fn = AsyncMock() | ||
| handler_md = _make_handler_metadata(handler_fn) | ||
| mock_handlers_registry.get_handlers_by_event_type = MagicMock( | ||
| return_value=[handler_md] | ||
| ) | ||
| event = _make_event() | ||
|
|
||
| extra_arg = MagicMock() | ||
| await call_event_hook( | ||
| event, EventType.OnLLMRequestEvent, extra_arg, timeout=5.0, extra_kwarg="test" | ||
| ) | ||
|
|
||
| handler_fn.assert_awaited_once() | ||
| call_args = handler_fn.call_args | ||
| assert call_args[0][0] is event | ||
| assert call_args[0][1] is extra_arg | ||
| assert call_args[1].get("extra_kwarg") == "test" | ||
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.