-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add plugin load&unload hook #5331
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ | |
| from .context import Context | ||
| from .filter.permission import PermissionType, PermissionTypeFilter | ||
| from .star import star_map, star_registry | ||
| from .star_handler import star_handlers_registry | ||
| from .star_handler import EventType, star_handlers_registry | ||
| from .updator import PluginUpdator | ||
|
|
||
| try: | ||
|
|
@@ -772,6 +772,19 @@ async def load( | |
| if hasattr(metadata.star_cls, "initialize") and metadata.star_cls: | ||
| await metadata.star_cls.initialize() | ||
|
|
||
| # 触发插件加载事件 | ||
| handlers = star_handlers_registry.get_handlers_by_event_type( | ||
| EventType.OnPluginLoadedEvent, | ||
| ) | ||
| for handler in handlers: | ||
| try: | ||
| logger.info( | ||
| f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", | ||
| ) | ||
| await handler.handler(metadata) | ||
| except Exception: | ||
| logger.error(traceback.format_exc()) | ||
PyuraMazo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| except BaseException as e: | ||
| logger.error(f"----- 插件 {root_dir_name} 载入失败 -----") | ||
| errors = traceback.format_exc() | ||
|
|
@@ -1159,6 +1172,19 @@ async def _terminate_plugin(star_metadata: StarMetadata) -> None: | |
| elif "terminate" in star_metadata.star_cls_type.__dict__: | ||
| await star_metadata.star_cls.terminate() | ||
|
|
||
| # 触发插件卸载事件 | ||
| handlers = star_handlers_registry.get_handlers_by_event_type( | ||
| EventType.OnPluginUnloadedEvent, | ||
| ) | ||
| for handler in handlers: | ||
| try: | ||
| logger.info( | ||
| f"hook(on_plugin_unloaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", | ||
| ) | ||
| await handler.handler(star_metadata) | ||
| except Exception: | ||
| logger.error(traceback.format_exc()) | ||
|
Comment on lines
+1179
to
+1186
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the |
||
|
|
||
| async def turn_on_plugin(self, plugin_name: str) -> None: | ||
| plugin = self.context.get_registered_star(plugin_name) | ||
| if plugin is None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newly introduced plugin lifecycle hooks are executed sequentially and awaited while holding the global plugin management lock (
_pm_lock). This creates a Denial of Service (DoS) vulnerability, as a malicious or poorly implemented plugin could indefinitely block the entire plugin system by providing a hook that never returns. Additionally, when triggering plugin load hooks, it's recommended to include the plugin's name (metadata.name) in the logs and explicitly state which plugin's hook failed during exception handling to improve observability and troubleshooting.