Skip to content

fix: resolve all waiters for plugin loads - #2955

Open
mikamikasuki wants to merge 9 commits into
Acode-Foundation:mainfrom
mikamikasuki:fix/2936-multiple-plugin-waiters
Open

mikamikasuki wants to merge 9 commits into
Acode-Foundation:mainfrom
mikamikasuki:fix/2936-multiple-plugin-waiters

Conversation

@mikamikasuki

Copy link
Copy Markdown

Fixes #2936

waitForPlugin() stored one resolver per plugin ID. When multiple callers waited for the same plugin, later calls replaced earlier resolvers and left those promises pending.

This change keeps all pending callers for each plugin ID, resolves them when that plugin loads, and rejects them all with the existing plugin-specific error when initial plugin loading completes. The already-loaded fast path remains unchanged.

Regression coverage verifies that multiple callers resolve together and that all waiters receive the correct error when loading fails.

Validation:

  • vitest run tests/unit/pluginWaiters.test.js
  • biome check src/lib/acode.js src/lib/pluginWaiters.js tests/unit/pluginWaiters.test.js
  • git diff --check

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge, with two non-blocking issues in error messages and enable feedback.

Findings

  1. P2 Errors lose the plugin name ▶
  2. P2 Enable reports success after timeout ▶

Summary

The PR shares one pending promise among callers waiting for the same plugin and connects success and failure callbacks to that promise.

  • Theme-only loading leaves regular-plugin waiters pending.
  • Background loads stay tracked after the startup timeout.
  • Enable actions now use the central loader.
  • Two non-blocking issues remain: failure messages lose the plugin name, and enable actions report success after a timeout.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Wait for plugin] --> B{Already loaded?}
  B -->|Yes| C[Resolve true]
  B -->|No| D{Startup complete and not loading?}
  D -->|Yes| E[Reject with plugin-specific error]
  D -->|No| F[Share pending promise]
  F --> G{Load result}
  G -->|Success| C
  G -->|Failure| H[Reject pending callers]
  G -->|Still loading after timeout| F
Loading

Reviews (2) · Last reviewed commit: "Cover plugin waiter lifecycle integratio..." · Reviewed by Greptile

Comment thread src/lib/acode.js Outdated
);
}
this.#pluginWatchers = {};
this.#pluginWatchers.rejectAll(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This callback also fires at the end of the theme-only pass (loadPlugins(true) in main.js:766), which runs before the main loadPlugins() pass. Any waiter registered by then (e.g. by a theme plugin waiting on a regular plugin) gets rejected with "failed to load" even though that plugin hasn't been attempted yet.

Could we only reject when !loadOnlyTheme, or pass that flag through to the callback?

Comment thread src/lib/acode.js Outdated
this.#pluginWatchers.resolve(pluginId);
}

[onPluginsLoadCompleteCallback]() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Timed-out plugins: loadPluginWithTimeout returns false after 15s but keeps loading in the background. loadPlugins then completes and rejects its waiters here. When the plugin finishes later, markPluginLoaded → resolve(pluginId) finds nothing, so dependents never initialize even though the plugin is running.

Maybe skip rejecting plugins that timed out but haven't settled, or defer their rejection until PLUGIN_DISABLE_TIMEOUT.

Comment thread src/lib/acode.js
);
}

waitForPlugin(pluginId) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the initial load has already completed (isInitialPluginLoadComplete()), a call for a plugin that's broken/disabled/not installed registers a waiter that will never settle — no further onPluginsLoadCompleteCallback runs, so the caller hangs silently.

Since this function is being rewritten anyway, it'd be good to reject immediately in that case.

Related: with the new Set, repeated calls for a never-loaded ID now accumulate entries for the rest of the session (previously it was overwritten, so bounded at one per ID). Settling late callers immediately fixes both.

Comment thread src/lib/acode.js Outdated
reject,
};
});
if (LOADED_PLUGINS.has(pluginId)) return Promise.resolve(true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: the fast path resolves with true, but the waiter path resolves with undefined (waiter.resolve() in PluginWaiters). Code like if (await acode.waitForPlugin("dep")) init(); behaves differently depending on load order. Suggest waiter.resolve(true) and updating the test accordingly.

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.

Also keep this in mind that, this fast reject only checks LOADED_PLUGINS / IN_FLIGHT_PLUGINS, but re-enabling a plugin from the Plugins page (pages/plugins/plugins.js L812) or the sidebar (sidebarApps/extensions/index.js L1043) calls loadPlugin(id) directly, which never touches either set and never fires onPluginLoadCallback. So if B depends on A and the user re-enables A then B, B's waitForPlugin("A") now rejects with "failed to load" even though A is running (earlier it just hung). Maybe route those call sites through loadPluginWithTimeout? Fine as a follow-up too, just flagging since this PR adds the early reject.

Comment thread src/lib/pluginWaiters.js Outdated
waiters = new Set();
this.#waiters.set(pluginId, waiters);
}
waiters.add({ resolve, reject });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggestion: rather than a Set of separate { resolve, reject } objects, store one deferred per plugin ID and hand every caller the same promise:

waitFor(pluginId) {
	let deferred = this.#waiters.get(pluginId);
	if (!deferred) {
		deferred = Promise.withResolvers();
		this.#waiters.set(pluginId, deferred);
	}
	return deferred.promise;
}

resolve/rejectAll then become single calls with no inner loops. (Check Promise.withResolvers support on the minimum WebView; a small manual deferred works too.)

Comment thread src/lib/pluginWaiters.js Outdated
rejectAll(getError) {
for (const [pluginId, waiters] of this.#waiters) {
this.#waiters.delete(pluginId);
for (const waiter of waiters) waiter.reject(getError(pluginId));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

getError(pluginId) is called per waiter inside the loop, after the entry has already been deleted. If it ever throws, the remaining waiters for this plugin (and all later plugins) are stranded, and the exception propagates into loadPlugins. Building the error once per plugin before the inner loop avoids that and avoids redundant allocations.

Comment thread tests/unit/pluginWaiters.test.js Outdated
@@ -0,0 +1,31 @@
import { expect, it } from "vitest";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These tests cover PluginWaiters in isolation, but none of the interesting failure modes are here — they're in how acode.waitForPlugin is wired to loadPlugins (theme-only pass rejection, timed-out plugins that load later, calls after initial load). Would be worth adding at least one test exercising waitForPlugin + the load callbacks.

const pending = Promise.allSettled([first, second, third]);

waiters.rejectAll((pluginId) => new Error(`Plugin '${pluginId}' failed to load.`));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: this file isn't Biome-formatted (several lines exceed the 80-col width). It slips through because biome.json files.includes only covers src/** and utils/**, so the biome check mentioned in the PR description doesn't actually check it. Running biome format --write on it explicitly would keep it consistent.

@mikamikasuki
mikamikasuki force-pushed the fix/2936-multiple-plugin-waiters branch from 1cebd0a to 184a04c Compare October 21, 2026 06:28
@@ -0,0 +1,75 @@
import { describe, expect, it } from "vitest";

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.

These still cover only PluginWaiters in isolation. The cases that actually broke are in the wiring (theme-only pass not rejecting, a timed-out in-flight plugin surviving rejectAll and resolving later, the fast reject after load completes). One test that mocks loadPlugin and drives loadPlugins() + acode.waitForPlugin() together would cover it.

Comment thread src/lib/loadPlugins.js Outdated

acode[onPluginsLoadCompleteCallback]();
if (!loadOnlyTheme) {
acode[onPluginsLoadCompleteCallback]?.(

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.

Nit: if anything above throws (e.g. lsDir on PLUGIN_DIR), this call is skipped but finally still sets initialPluginLoadComplete = true, so already queued waiters never settle. Moving the rejectAll into finally (still behind !loadOnlyTheme) would close that.

Comment thread src/lib/acode.js Outdated
waitForPlugin(pluginId) {
if (LOADED_PLUGINS.has(pluginId)) return Promise.resolve(true);
if (isInitialPluginLoadComplete() && !isPluginLoading(pluginId)) {
return Promise.reject(new Error(`Plugin '${pluginId}' failed to load.`));

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.

Nit: Plugin '${pluginId}' failed to load. is now built in 3 places, a small helper would keep it in one spot. Also ?. is used for the new callbacks but not for onPluginLoadCallback in markPluginLoaded, would be good to keep it consistent.

Comment thread src/lib/acode.js
}
this.#pluginWatchers = {};
[onPluginLoadFailedCallback](pluginId, error) {
this.#pluginWatchers.reject(pluginId, error || pluginLoadError(pluginId));

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.

P2 Errors lose the plugin name

When a plugin throws during loading, onPluginLoadFailedCallback rejects its waiters with the original error instead of the existing plugin-specific error promised by this PR. An error such as “Syntax error” no longer identifies which plugin failed. Reject with pluginLoadError(pluginId) and keep the original error as its cause.

delete disabledMap[id];
settings.update({ pluginsDisabled: disabledMap }, false);
await loadPlugin(id);
await loadPluginWithTimeout(id);

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.

P2 Enable reports success after timeout

loadPluginWithTimeout returns false after 15 seconds, but this handler ignores the result and announces “Plugin enabled.” It then marks the plugin enabled in every displayed list. If the load never finishes, the loader later disables it, leaving the page showing success for an inactive plugin.

Check the result before announcing success or updating the lists. The sidebar enable handler also ignores this result.

Knowledge Base Used: Plugin lifecycle and extension management

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Race Condition in acode.waitForPlugin

3 participants