Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: dedupe concurrent CivitAI installs (#4917)

[._.]/ Adam Eivyd6751bce6b8a
diff --git a/server/services/loras.js b/server/services/loras.js
index 5970440f12..7f57036adf 100644
--- a/server/services/loras.js
+++ b/server/services/loras.js
@@ -67,6 +67,11 @@ import { getSettings } from './settings.js';
const SIDECAR_SUFFIX = '.metadata.json';
+// One install attempt per Civitai model at a time. The slot is a promise so a
+// duplicate submit shares the original result instead of racing it to the same
+// destination files. Different model ids remain independent.
+const civitaiInstalls = new Map();
+
const sidecarPath = (loraFilename) => join(PATHS.loras, `${loraFilename}${SIDECAR_SUFFIX}`);
// Reads the sidecar JSON next to a LoRA file. Returns `null` when the
@@ -517,8 +522,7 @@ const verifyDownloadedLora = async (destPath, { expectedSha256 = null, source =
// Install a LoRA from a Civitai URL. Returns the new sidecar JSON so the
// client can render it immediately without a second list round-trip.
-export const installFromCivitai = async (input, { fetchImpl = fetch } = {}) => {
- const { modelId, versionId } = parseCivitaiUrl(input?.url);
+const performCivitaiInstall = async (input, { modelId, versionId, fetchImpl }) => {
const apiKey = (typeof input?.apiKey === 'string' && input.apiKey.trim()) || (await resolveCivitaiKey());
const model = await fetchCivitaiModel(modelId, { apiKey, fetchImpl });
const version = pickVersion(model, versionId);
@@ -591,6 +595,19 @@ export const installFromCivitai = async (input, { fetchImpl = fetch } = {}) => {
return sidecar;
};
+export const installFromCivitai = async (input, { fetchImpl = fetch } = {}) => {
+ const { modelId, versionId } = parseCivitaiUrl(input?.url);
+ const active = civitaiInstalls.get(modelId);
+ if (active) return active;
+
+ const attempt = performCivitaiInstall(input, { modelId, versionId, fetchImpl });
+ const tracked = attempt.finally(() => {
+ if (civitaiInstalls.get(modelId) === tracked) civitaiInstalls.delete(modelId);
+ });
+ civitaiInstalls.set(modelId, tracked);
+ return tracked;
+};
+
// Set of recognized LoRA families an HF import may target (image runners +
// video families). Used to validate a user-supplied `family` override.
const HF_LORA_FAMILY_VALUES = new Set(HF_LORA_FAMILIES);
diff --git a/server/services/loras.test.js b/server/services/loras.test.js
index 4567aa6fb6..ee9f9bd78a 100644
--- a/server/services/loras.test.js
+++ b/server/services/loras.test.js
@@ -436,6 +436,56 @@ describe('installFromCivitai', () => {
expect(calls).toHaveLength(2);
});
+ it('shares one in-flight install between duplicate model submissions', async () => {
+ const downloadedBytes = validSafetensors();
+ let releaseMetadata;
+ let metadataCalls = 0;
+ let downloadCalls = 0;
+ const fetchImpl = async (url) => {
+ if (url.startsWith('https://civitai.com/api/v1/models/2600698')) {
+ metadataCalls += 1;
+ return new Promise((resolve) => {
+ releaseMetadata = () => resolve(mockJsonResponse(FAKE_MODEL));
+ });
+ }
+ if (url.startsWith('https://civitai.com/api/download/models/7')) {
+ downloadCalls += 1;
+ const stream = new ReadableStream({
+ start(c) { c.enqueue(new Uint8Array(downloadedBytes)); c.close(); },
+ });
+ return { ok: true, status: 200, body: stream };
+ }
+ throw new Error(`unexpected fetch: ${url}`);
+ };
+
+ const first = lorasService.installFromCivitai({ url: 'https://civitai.com/models/2600698' }, { fetchImpl });
+ await vi.waitFor(() => expect(releaseMetadata).toBeTypeOf('function'));
+ const second = lorasService.installFromCivitai({ url: 'https://civitai.com/models/2600698' }, { fetchImpl });
+
+ releaseMetadata();
+ const [firstResult, secondResult] = await Promise.all([first, second]);
+ expect(secondResult).toEqual(firstResult);
+ expect(metadataCalls).toBe(1);
+ expect(downloadCalls).toBe(1);
+ });
+
+ it('releases the in-flight slot after a failed install so retry can proceed', async () => {
+ let metadataCalls = 0;
+ const fetchImpl = async () => {
+ metadataCalls += 1;
+ if (metadataCalls === 1) throw new Error('metadata unavailable');
+ return mockJsonResponse({ ...FAKE_MODEL, type: 'Checkpoint' });
+ };
+
+ await expect(
+ lorasService.installFromCivitai({ url: 'https://civitai.com/models/2600698' }, { fetchImpl }),
+ ).rejects.toThrow(/metadata unavailable/);
+ await expect(
+ lorasService.installFromCivitai({ url: 'https://civitai.com/models/2600698' }, { fetchImpl }),
+ ).rejects.toThrow(/not a LoRA/);
+ expect(metadataCalls).toBe(2);
+ });
+
it('refuses to install non-LoRA model types', async () => {
const fetchImpl = async () => (mockJsonResponse({ ...FAKE_MODEL, type: 'Checkpoint' }));
await expect(