-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix:plugin uninstall file handle leak #8654
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
lingyun14beta
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
lingyun14beta:fixenhance-plugin-uninstall-cleanup
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.
+124
−2
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1549,6 +1549,11 @@ async def uninstall_plugin( | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| await self._unbind_plugin(plugin_name, plugin.module_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_module_prefix = plugin.module_path.rsplit(".", 1)[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_dir = os.path.join(ppath, root_dir_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await self._cancel_plugin_tasks(plugin_module_prefix) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._release_plugin_font_handles(plugin, plugin_dir) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 删除插件文件夹 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| remove_dir(os.path.join(ppath, root_dir_name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1564,6 +1569,73 @@ async def uninstall_plugin( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| delete_data=delete_data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def _cancel_plugin_tasks(self, plugin_module_prefix: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """取消并等待属于指定插件模块的所有 asyncio tasks。""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_tasks = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for task in asyncio.all_tasks(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if task.done(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| coro = task.get_coro() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| frame = getattr(coro, "cr_frame", None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if frame is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| mod_name = frame.f_globals.get("__name__", "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if mod_name == plugin_module_prefix or mod_name.startswith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_module_prefix + "." | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_tasks.append(task) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1575
to
+1586
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. 任务取消安全性与 Frame 兼容性:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not plugin_tasks: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.debug( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"取消插件 {plugin_module_prefix} 的 {len(plugin_tasks)} 个 asyncio tasks" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for task in plugin_tasks: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| task.cancel() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await asyncio.wait_for( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| asyncio.gather(*plugin_tasks, return_exceptions=True), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout=5.0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except asyncio.TimeoutError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"插件 {plugin_module_prefix} 的部分 tasks 在 5 秒内未能取消," | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "可能导致文件句柄未释放。" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await asyncio.sleep(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _release_plugin_font_handles(self, plugin, plugin_dir: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """释放插件目录下字体文件的句柄,确保删除目录前文件不被占用。""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import gc | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PIL import ImageFont | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.star_cls = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.module = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.star_cls_type = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_dir_norm = os.path.normcase(os.path.normpath(plugin_dir)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self_locals_id = id(locals()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for obj in gc.get_objects(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(obj, ImageFont.FreeTypeFont): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| font_path = getattr(obj, "path", None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(font_path, str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not os.path.normcase(os.path.normpath(font_path)).startswith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin_dir_norm + os.sep | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.debug(f"释放字体句柄: {font_path}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for referrer in gc.get_referrers(obj): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(referrer, dict) or id(referrer) == self_locals_id: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in [k for k, v in referrer.items() if v is obj]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| referrer[k] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| gc.collect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def uninstall_failed_plugin( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dir_name: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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
Oops, something went wrong.
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.
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.
issue (complexity): Consider replacing the global task and GC introspection logic with explicit per-plugin registries for tasks and fonts to simplify uninstall handling.
The main complexity comes from the global introspection in
_cancel_plugin_tasksand_release_plugin_font_handles. Both can be simplified by explicit per‑plugin tracking while keeping the same behavior.1. Replace
_cancel_plugin_tasksintrospection with explicit trackingInstead of scanning
asyncio.all_tasks()and inspecting frames, track tasks when they are created. For example, add a per‑plugin registry and a helper:Then
_cancel_plugin_tasksbecomes much simpler and explicit:Callers inside plugin code (or the plugin manager) should use
create_plugin_taskinstead ofasyncio.create_task, preserving all existing behavior while avoiding frame inspection and global task scanning.2. Replace GC sweeping for fonts with an explicit font registry
Instead of walking
gc.get_objects()/get_referrers, ensure fonts loaded by a plugin are tracked when created. For example, centralize font loading for plugins:Then
_release_plugin_font_handlescan directly release/clear them:If you can’t modify plugin code directly, you can still centralize font creation in the manager (e.g. exposing a
get_fonthelper that plugins are required to use) rather than sweeping the entire object graph.This keeps the uninstall semantics (cancel plugin tasks, release plugin font handles) but removes the dependence on CPython GC internals and global introspection, making the behavior easier to reason about and maintain.