Skip to content

Commit ba4ff7a

Browse files
authored
fix(plugins): recover official catalog after failed loads (#126815)
1 parent b3a41e6 commit ba4ff7a

2 files changed

Lines changed: 77 additions & 19 deletions

File tree

‎src/plugins/management-service.lifecycle-cache.test.ts‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createDeferred } from "../../test/helpers/promise.js";
23
import {
34
clearPluginMetadataLifecycleCaches,
45
registerPluginMetadataProcessMemoLifecycleClear,
@@ -51,8 +52,13 @@ function metadataSnapshot(pluginId?: string) {
5152
}
5253

5354
describe("plugin management catalog lifecycle", () => {
54-
it("serves the first plugins.list load from prewarmed metadata and official catalog caches", async () => {
55+
beforeEach(() => {
56+
mocks.metadata.mockReset();
57+
mocks.officialCatalog.mockReset();
5558
clearPluginMetadataLifecycleCaches();
59+
});
60+
61+
it("serves the first plugins.list load from prewarmed metadata and official catalog caches", async () => {
5662
mocks.metadata
5763
.mockReturnValueOnce(metadataSnapshot())
5864
.mockReturnValueOnce(metadataSnapshot("fresh-plugin"));
@@ -99,4 +105,67 @@ describe("plugin management catalog lifecycle", () => {
99105
expect.objectContaining({ id: "fresh-plugin", installed: true, enabled: true }),
100106
]);
101107
});
108+
109+
it("retries a failed official catalog prewarm and keeps the recovered catalog process-stable", async () => {
110+
mocks.metadata.mockReturnValue(metadataSnapshot());
111+
mocks.officialCatalog
112+
.mockRejectedValueOnce(new Error("transient catalog bootstrap"))
113+
.mockResolvedValueOnce({ source: "hosted", entries: [] });
114+
115+
await expect(listManagedPlugins({ config: {}, env: {} })).rejects.toThrow(
116+
"transient catalog bootstrap",
117+
);
118+
await expect(listManagedPlugins({ config: {}, env: {} })).resolves.toMatchObject({
119+
plugins: [],
120+
});
121+
await expect(listManagedPlugins({ config: {}, env: {} })).resolves.toMatchObject({
122+
plugins: [],
123+
});
124+
expect(mocks.officialCatalog).toHaveBeenCalledTimes(2);
125+
});
126+
127+
it("keeps a refreshed catalog when an older lifecycle generation rejects later", async () => {
128+
const retiredCatalog = createDeferred<{ source: "hosted"; entries: never[] }>();
129+
mocks.metadata.mockReturnValue(metadataSnapshot());
130+
mocks.officialCatalog
131+
.mockReturnValueOnce(retiredCatalog.promise)
132+
.mockResolvedValueOnce({ source: "hosted", entries: [] });
133+
134+
const retiredLoad = listManagedPlugins({ config: {}, env: {} });
135+
const retiredFailure = expect(retiredLoad).rejects.toThrow("retired catalog bootstrap");
136+
await Promise.resolve();
137+
expect(mocks.officialCatalog).toHaveBeenCalledTimes(1);
138+
139+
clearPluginMetadataLifecycleCaches();
140+
141+
await expect(listManagedPlugins({ config: {}, env: {} })).resolves.toMatchObject({
142+
plugins: [],
143+
});
144+
retiredCatalog.reject(new Error("retired catalog bootstrap"));
145+
await retiredFailure;
146+
147+
await expect(listManagedPlugins({ config: {}, env: {} })).resolves.toMatchObject({
148+
plugins: [],
149+
});
150+
expect(mocks.officialCatalog).toHaveBeenCalledTimes(2);
151+
});
152+
153+
it("keeps a successfully resolved bundled-fallback catalog process-stable", async () => {
154+
mocks.metadata.mockReturnValue(metadataSnapshot());
155+
mocks.officialCatalog.mockResolvedValueOnce({
156+
source: "bundled-fallback",
157+
entries: [],
158+
error: "hosted feed unavailable",
159+
});
160+
161+
const first = await listManagedPlugins({ config: {}, env: {} });
162+
const second = await listManagedPlugins({ config: {}, env: {} });
163+
164+
expect(first.diagnostics).toContainEqual({
165+
level: "warn",
166+
message: "Official plugin catalog fallback: hosted feed unavailable",
167+
});
168+
expect(second).toEqual(first);
169+
expect(mocks.officialCatalog).toHaveBeenCalledOnce();
170+
});
102171
});

‎src/plugins/management-service.ts‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ import {
7373
type HostedOfficialExternalPluginCatalogLoadResult,
7474
type OfficialExternalPluginCatalogEntry,
7575
} from "./official-external-plugin-catalog.js";
76+
import { createConfigScopedPromiseLoader } from "./plugin-cache-primitives.js";
7677
import { withPluginLifecycleLease } from "./plugin-lifecycle-lease.js";
77-
import { registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle.js";
7878
import {
7979
loadPluginMetadataSnapshot,
8080
resolvePluginMetadataSnapshot,
@@ -254,19 +254,15 @@ type OfficialCatalogResult = Pick<HostedOfficialExternalPluginCatalogLoadResult,
254254
hostedFeaturedAuthoritative?: boolean;
255255
};
256256

257-
let officialCatalogCache:
258-
| { key: string; result: Promise<HostedOfficialExternalPluginCatalogLoadResult> }
259-
| undefined;
260-
261-
const OFFICIAL_CATALOG_CACHE_KEY = "built-in";
257+
const officialCatalogLoader = createConfigScopedPromiseLoader(() =>
258+
loadConfiguredHostedOfficialExternalPluginCatalogEntries(),
259+
);
262260

263261
/** Clear the process-stable hosted catalog snapshot after an explicit owner reload. */
264262
export function clearManagedPluginOfficialCatalogCache(): void {
265-
officialCatalogCache = undefined;
263+
officialCatalogLoader.clear();
266264
}
267265

268-
registerPluginMetadataProcessMemoLifecycleClear(clearManagedPluginOfficialCatalogCache);
269-
270266
function resolveCatalogManifestIcon(manifest: unknown): string | undefined {
271267
if (!manifest || typeof manifest !== "object") {
272268
return undefined;
@@ -398,14 +394,7 @@ function overlayBundledOfficialPluginCatalogMetadata(
398394
}
399395

400396
async function loadOfficialCatalog(): Promise<OfficialCatalogResult> {
401-
const key = OFFICIAL_CATALOG_CACHE_KEY;
402-
if (officialCatalogCache?.key !== key) {
403-
officialCatalogCache = {
404-
key,
405-
result: loadConfiguredHostedOfficialExternalPluginCatalogEntries(),
406-
};
407-
}
408-
const result = await officialCatalogCache.result;
397+
const result = await officialCatalogLoader.load();
409398
const hostedFeaturedAuthoritative =
410399
result.source === "hosted" || result.source === "hosted-snapshot";
411400
return {

0 commit comments

Comments
 (0)