fix(plugin): close cancellable install lifecycle gaps

This commit is contained in:
InfinityPacer
2026-08-23 14:26:42 +08:00
parent 0378eabb8f
commit 6c2817e383
13 changed files with 240 additions and 47 deletions
+6 -2
View File
@@ -68,6 +68,7 @@ class _InstallState:
checkpoint: Any = None
stage: str = "package_checkpoint"
package_installed: bool = False
installed_list_touched: bool = False
installed_list_persisted: bool = False
runtime_touched: bool = False
registrations_touched: bool = False
@@ -210,6 +211,8 @@ class PluginInstallCommand:
if plugin_id not in installed_plugins:
updated_plugins = [*installed_plugins, plugin_id]
try:
# 写入方可能在返回前已经提交;取消时按已触碰处理,恢复原清单是幂等的。
state.installed_list_touched = True
await self._installed_plugins_writer(updated_plugins)
installed_list_persisted = True
state.installed_list_persisted = True
@@ -267,9 +270,10 @@ class PluginInstallCommand:
checkpoint_cleanup_error = ""
state.stage = "checkpoint_commit"
# 运行态和注册已完成,后续只清理临时快照,不再把取消当作未提交安装回滚。
state.committed = True
try:
await self._package_committer(checkpoint)
state.committed = True
except Exception as err:
checkpoint_cleanup_error = str(err)
@@ -328,7 +332,7 @@ class PluginInstallCommand:
stage=state.stage,
message="插件安装已取消",
package_installed=state.package_installed,
installed_list_persisted=state.installed_list_persisted,
installed_list_persisted=state.installed_list_touched,
runtime_touched=state.runtime_touched,
registrations_touched=state.registrations_touched,
)
+1 -18
View File
@@ -4,8 +4,7 @@ from __future__ import annotations
import asyncio
import threading
from contextlib import asynccontextmanager, contextmanager
from typing import Iterator
from contextlib import asynccontextmanager
class PluginLifecycleCoordinator:
@@ -58,21 +57,6 @@ class PluginLifecycleCoordinator:
finally:
self._release_plugin(plugin_id)
@contextmanager
def hold_sync(self, plugin_id: str) -> Iterator[None]:
"""同步持有单个插件的生命周期资格。"""
normalized_id = self._normalize(plugin_id)
if not normalized_id:
raise ValueError("插件ID不能为空")
with self._condition:
while self._startup_active or normalized_id in self._active_plugins:
self._condition.wait()
self._active_plugins.add(normalized_id)
try:
yield
finally:
self._release_plugin(normalized_id)
@asynccontextmanager
async def hold_startup(self):
"""异步持有启动同步的全局资格,阻止安装请求穿过启动收口。"""
@@ -85,4 +69,3 @@ class PluginLifecycleCoordinator:
plugin_lifecycle = PluginLifecycleCoordinator()