Skip to content

Commit 98b89eb

Browse files
fix:fix the issue where incomplete cleanup of residual plugins occurs… (#5462)
* fix:fix the issue where incomplete cleanup of residual plugins occurs in the failed loading of plugins * fix:ruff format,apply bot suggestions * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 39b9e55 commit 98b89eb

1 file changed

Lines changed: 41 additions & 10 deletions

File tree

astrbot/core/star/star_manager.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,33 @@ def _purge_modules(
388388
except KeyError:
389389
logger.warning(f"模块 {module_name} 未载入")
390390

391+
def _cleanup_plugin_state(self, dir_name: str) -> None:
392+
plugin_root_name = "data.plugins."
393+
394+
# 清理 sys.modules
395+
for key in list(sys.modules.keys()):
396+
if key.startswith(f"{plugin_root_name}{dir_name}"):
397+
logger.info(f"清除了插件{dir_name}中的{key}模块")
398+
del sys.modules[key]
399+
400+
possible_paths = [
401+
f"{plugin_root_name}{dir_name}.main",
402+
f"{plugin_root_name}{dir_name}.{dir_name}",
403+
]
404+
405+
# 清理 handlers
406+
for path in possible_paths:
407+
handlers = star_handlers_registry.get_handlers_by_module_name(path)
408+
for handler in handlers:
409+
star_handlers_registry.remove(handler)
410+
logger.info(f"清理处理器: {handler.handler_name}")
411+
412+
# 清理工具
413+
for tool in list(llm_tools.func_list):
414+
if tool.handler_module_path in possible_paths:
415+
llm_tools.func_list.remove(tool)
416+
logger.info(f"清理工具: {tool.name}")
417+
391418
async def reload_failed_plugin(self, dir_name):
392419
"""
393420
重新加载未注册(加载失败)的插件
@@ -398,17 +425,21 @@ async def reload_failed_plugin(self, dir_name):
398425
- success (bool): 重载是否成功
399426
- error_message (str|None): 错误信息,成功时为 None
400427
"""
428+
401429
async with self._pm_lock:
402-
if dir_name in self.failed_plugin_dict:
403-
success, error = await self.load(specified_dir_name=dir_name)
404-
if success:
405-
self.failed_plugin_dict.pop(dir_name, None)
406-
if not self.failed_plugin_dict:
407-
self.failed_plugin_info = ""
408-
return success, None
409-
else:
410-
return False, error
411-
return False, "插件不存在于失败列表中"
430+
if dir_name not in self.failed_plugin_dict:
431+
return False, "插件不存在于失败列表中"
432+
433+
self._cleanup_plugin_state(dir_name)
434+
435+
success, error = await self.load(specified_dir_name=dir_name)
436+
if success:
437+
self.failed_plugin_dict.pop(dir_name, None)
438+
if not self.failed_plugin_dict:
439+
self.failed_plugin_info = ""
440+
return success, None
441+
else:
442+
return False, error
412443

413444
async def reload(self, specified_plugin_name=None):
414445
"""重新加载插件

0 commit comments

Comments
 (0)