Skip to content

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

Open
zzz27578 wants to merge 1 commit into
AstrBotDevs:masterfrom
zzz27578:fix/plugin-update-consistency
Open

zzz27578 wants to merge 1 commit into
AstrBotDevs:masterfrom
zzz27578:fix/plugin-update-consistency

Conversation

@zzz27578

@zzz27578 zzz27578 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Two plugin update requests can replace the same directory before either reload finishes. In addition, reloading the replacement can remove the plugin from the runtime registry, so the old source record remains even though the new files are installed.

Follow-up to the concurrency and install-source findings in #11072. Entry discovery and entry-name changes remain in that PR.

Modifications / 改动点

  • Serialize download, replacement, dependency installation, and reload for the same installation directory. Different plugins can still download in parallel. Ignore repeated names in one batch, in input order.

  • Persist the source by installation directory once replacement succeeds, without relying on the plugin being re-registered. A failed download or replacement does not advance the record.

  • Return the existing update error on reload failure, remove the extra single-update reload, and sync skills after attempted updates. This does not add rollback of the updated files.

  • This is NOT a breaking change.

Test Results / 测试结果

  • The regression cases reproduce out-of-order reloads and the single/batch failure cases before the fix.
  • Windows / Python 3.12: python -m pytest tests/test_plugin_manager.py tests/test_fastapi_v1_dashboard.py tests/test_updater_socks.py -q — 375 passed (one existing audioop deprecation warning). The focused update tests were rerun on the latest master base and all 8 passed.
  • ruff format --check ., ruff check ., and git diff --check passed. The tests extend an existing file.

Checklist

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

Summary by Sourcery

Serialize overlapping plugin updates and persist installation sources based on successful file replacement.

Bug Fixes:

  • Prevent concurrent updates from replacing and reloading the same plugin directory out of order.
  • Keep plugin installation-source records current after files are replaced, even when reloading removes the plugin from the runtime registry.
  • Preserve update failures and avoid advancing installation-source records when downloads fail.

Enhancements:

  • Allow updates for different plugin directories to proceed concurrently while serializing overlapping updates.
  • Deduplicate plugin names in batch updates and synchronize skills after attempted updates.

Tests:

  • Add regression coverage for overlapping updates and installation-source persistence across single and batch update failures.

@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 ✨

if plugin.reserved:
raise Exception("该插件是 AstrBot 保留插件,无法更新。")

await self._updater.update(

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 · 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.

Prompt for AI agents
In `astrbot/core/star/star_manager.py` at line 1967:

**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.

EventType.OnPluginErrorEvent: "插件报错时",
}
self._logo_cache: dict[str, str] = {}
self._install_source_lock = asyncio.Lock()

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 · 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.

Prompt for AI agents
In `astrbot/dashboard/services/plugin_service.py` at line 113:

**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`.

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.

**new_source,
"root_dir_name": TEST_PLUGIN_DIR,
"updated_at": record["updated_at"],
}

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 · 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.

Prompt for AI agents
In `tests/test_plugin_manager.py` at line 2212:

**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.

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.

1 participant