Skip to content

Commit 300a73a

Browse files
authored
fix(#4188): terminate the same plugin when install the plugin via file (#4250)
* fix(#4188): 从文件安装插件时先终止并解绑已存在的同名插件 * feat(star): 优化从文件安装插件的处理同名冲突逻辑,增加边缘检查
1 parent a5b9de3 commit 300a73a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

astrbot/core/star/star_manager.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,49 @@ async def install_plugin_from_file(self, zip_file_path: str):
944944
dir_name = os.path.basename(zip_file_path).replace(".zip", "")
945945
dir_name = dir_name.removesuffix("-master").removesuffix("-main").lower()
946946
desti_dir = os.path.join(self.plugin_store_path, dir_name)
947+
948+
# 第一步:检查是否已安装同目录名的插件,先终止旧插件
949+
existing_plugin = None
950+
for star in self.context.get_all_stars():
951+
if star.root_dir_name == dir_name:
952+
existing_plugin = star
953+
break
954+
955+
if existing_plugin:
956+
logger.info(f"检测到插件 {existing_plugin.name} 已安装,正在终止旧插件...")
957+
try:
958+
await self._terminate_plugin(existing_plugin)
959+
except Exception:
960+
logger.warning(traceback.format_exc())
961+
if existing_plugin.name and existing_plugin.module_path:
962+
await self._unbind_plugin(
963+
existing_plugin.name, existing_plugin.module_path
964+
)
965+
947966
self.updator.unzip_file(zip_file_path, desti_dir)
948967

968+
# 第二步:解压后,读取新插件的 metadata.yaml,检查是否存在同名但不同目录的插件
969+
try:
970+
new_metadata = self._load_plugin_metadata(desti_dir)
971+
if new_metadata and new_metadata.name:
972+
for star in self.context.get_all_stars():
973+
if (
974+
star.name == new_metadata.name
975+
and star.root_dir_name != dir_name
976+
):
977+
logger.warning(
978+
f"检测到同名插件 {star.name} 存在于不同目录 {star.root_dir_name},正在终止..."
979+
)
980+
try:
981+
await self._terminate_plugin(star)
982+
except Exception:
983+
logger.warning(traceback.format_exc())
984+
if star.name and star.module_path:
985+
await self._unbind_plugin(star.name, star.module_path)
986+
break # 只处理第一个匹配的
987+
except Exception as e:
988+
logger.debug(f"读取新插件 metadata.yaml 失败,跳过同名检查: {e!s}")
989+
949990
# remove the zip
950991
try:
951992
os.remove(zip_file_path)

0 commit comments

Comments
 (0)