Skip to content

fix(plugins): serialize updates and keep install sources current - #11353

Open
zzz27578 wants to merge 5 commits into
AstrBotDevs:masterfrom
zzz27578:fix/plugin-update-consistency
Open

zzz27578 wants to merge 5 commits into
AstrBotDevs:masterfrom
zzz27578:fix/plugin-update-consistency

Conversation

@zzz27578

@zzz27578 zzz27578 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Overlapping plugin updates can replace the same installation directory before either reload finishes. A reload failure can also remove the plugin from the runtime registry, leaving the old installation source attached to the new files. Updates can additionally recreate a directory after a concurrent uninstall has reported success.

Modifications / 改动点

  • Serialize update and uninstall operations for the same installation directory. Different plugins can still download concurrently; batch updates deduplicate plugin names in input order.
  • Persist installation sources by directory after file replacement, independently of reload success. Failed downloads do not advance the source record, and all source mutations share a lock to prevent lost writes.
  • Report reload failures through the existing error responses, remove the extra single-update reload, and synchronize skills after attempted updates. Uninstall removes the corresponding source record, including failed plugins.
  • Match failed-plugin uninstall requests against recorded directory names, reject path traversal, and constrain filesystem access to the resolved plugin directory.

Queued updates revalidate the plugin registration after acquiring the directory lock and fail if it disappeared. Automatic repair of failed plugins and rollback of updated files are outside this change. Because a failed reload leaves the replacement files installed, their source record remains current.

Related: #11072 covers missing entry modules and entry-name changes. This PR addresses the separate update concurrency and source persistence findings.

  • This is NOT a breaking change.

Test Results / 测试结果

Windows / Python 3.12:

python -m pytest tests/test_plugin_manager.py tests/test_fastapi_v1_dashboard.py tests/test_updater_socks.py -q
390 passed, 1 skipped, 1 warning in 88.76s

The warning is the existing audioop deprecation warning. The symlink case is skipped on Windows without symlink privileges. Regression cases cover overlapping updates, both update/uninstall orderings, source persistence after failed reloads, concurrent source mutations, source cleanup without live registration, timestamp advancement, and invalid uninstall paths. Tests extend existing files.

ruff format ., ruff check ., and git diff --check passed.

Checklist

  • No new features, dependencies, or API schemas.
  • Automated verification is included.
  • No WebUI or documentation changes are required.

Summary by Sourcery

Serialize plugin lifecycle operations and make installation source persistence safe across concurrent updates, reload failures, and uninstall operations.

Bug Fixes:

  • Prevent concurrent plugin updates and uninstalls from corrupting installations or recreating removed plugin directories.
  • Keep installation source records aligned with replaced plugin files, including when reload fails, while avoiding updates after failed downloads.
  • Return reload failures through existing update error handling and clean up source records when registered or failed plugins are uninstalled.
  • Reject unsafe failed-plugin directory paths and restrict filesystem operations to valid plugin directories.

Enhancements:

  • Deduplicate batch update requests while preserving input order and continue allowing independent plugins to update concurrently.
  • Synchronize installation-source mutations and skill refreshes across plugin changes.

Tests:

  • Add regression coverage for serialized updates and uninstalls, source persistence and concurrent mutations, cleanup without live registrations, batch deduplication, and invalid plugin paths.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/star/star_manager.py" line_range="1967" />
<code_context>
-        if plugin.reserved:
-            raise Exception("该插件是 AstrBot 保留插件,无法更新。")
-
-        await self._updater.update(
-            plugin,
-            proxy=proxy,
</code_context>
<issue_to_address>
**Uninstall can be undone by update**

When an uninstall removes a plugin while its update is downloading, `uninstall_plugin` does not acquire this per-directory update lock. If it removes the plugin while `_updater.update` is downloading, the updater can then recreate the directory; the subsequent reload can load that plugin again, so the uninstall caller sees success but the plugin is installed again.

Coordinate uninstall with the same installation-directory lock, and recheck that the plugin is still installed before committing or reloading an update.
</issue_to_address>

### Comment 2
<location path="astrbot/dashboard/services/plugin_service.py" line_range="113" />
<code_context>
             EventType.OnPluginErrorEvent: "插件报错时",
         }
         self._logo_cache: dict[str, str] = {}
+        self._install_source_lock = asyncio.Lock()

     @staticmethod
</code_context>
<issue_to_address>
**Plugin sources revert after concurrent writes**

When an install, source binding, or removal overlaps an update’s source refresh, `refresh_plugin_install_source_after_update`, `persist_plugin_install_source`, `bind_plugin_market_source`, and `remove_plugin_install_source` read and write the same records mapping without all using the shared lock, so a stale save overwrites another plugin’s record or a newer binding or removal.

Use `_install_source_lock` for every read-modify-write operation on the install-source records.

Also at `astrbot/dashboard/services/plugin_service.py:1557-1583`.
</issue_to_address>

### Comment 3
<location path="astrbot/core/star/star_manager.py" line_range="1962-1963" />
<code_context>
+        update_lock = self._update_locks.setdefault(plugin_path, asyncio.Lock())
+        async with update_lock:
+            plugin = self.context.get_registered_star(plugin_name)
+            if not plugin or plugin.root_dir_name != root_dir_name:
+                raise Exception("插件不存在。")
+            if plugin.reserved:
+                raise Exception("该插件是 AstrBot 保留插件,无法更新。")
</code_context>
<issue_to_address>
**Queued update cannot recover failed reload**

When two updates for the same plugin overlap and the first replacement fails to reload, the first update can replace the files and then fail to reload, causing `load` to remove the plugin from the runtime registry. A second request already waiting on the same directory lock then fails this registry check and never applies its update, leaving the installed files unrepaired even when its source contains a fix.

Allow the queued update to proceed using the captured installation directory and a safe reload path even if the preceding reload removed the plugin from the runtime registry.
</issue_to_address>

### Comment 4
<location path="tests/test_plugin_manager.py" line_range="2212" />
<code_context>
             EventType.OnPluginErrorEvent: "插件报错时",
         }
         self._logo_cache: dict[str, str] = {}
+        self._install_source_lock = asyncio.Lock()
</code_context>
<issue_to_address>
**Stale source timestamp passes**

When the source refresh writes a stale or malformed `updated_at` value, `test_update_plugin_persists_source_only_after_replacing_files` copies `record["updated_at"]` into its own expected value on line 2211, so a stale or malformed timestamp still passes the equality assertion.

Assert that `updated_at` parses as a timestamp and is newer than a fixed old timestamp in the source record.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 4 findings to address first, and after the updater replaces plugin files, the callback can persist new repository and download-source details before dependency installation or reload succeeds. If those steps fail, the changed source record remains and may influence a later update; reverting this code does not restore it, though the record is bounded and can be corrected.

Blocking findings: astrbot/core/star/star_manager.py:1967, astrbot/dashboard/services/plugin_service.py:113, astrbot/core/star/star_manager.py:1963, tests/test_plugin_manager.py:2212


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/star/star_manager.py Resolved
Comment on lines +1962 to +1963
if not plugin or plugin.root_dir_name != root_dir_name:
raise Exception("插件不存在。")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Queued update cannot recover failed reload

When two updates for the same plugin overlap and the first replacement fails to reload, the first update can replace the files and then fail to reload, causing load to remove the plugin from the runtime registry. A second request already waiting on the same directory lock then fails this registry check and never applies its update, leaving the installed files unrepaired even when its source contains a fix.

Allow the queued update to proceed using the captured installation directory and a safe reload path even if the preceding reload removed the plugin from the runtime registry.

Prompt for AI agents
In `astrbot/core/star/star_manager.py` at lines 1962-1963:

**Queued update cannot recover failed reload**

When two updates for the same plugin overlap and the first replacement fails to reload, the first update can replace the files and then fail to reload, causing `load` to remove the plugin from the runtime registry. A second request already waiting on the same directory lock then fails this registry check and never applies its update, leaving the installed files unrepaired even when its source contains a fix.

Allow the queued update to proceed using the captured installation directory and a safe reload path even if the preceding reload removed the plugin from the runtime registry.

Comment thread tests/test_plugin_manager.py Resolved
Comment thread astrbot/core/star/star_manager.py Fixed
Comment thread astrbot/core/star/star_manager.py Fixed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants