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: address CivitAI install review (#4917)

[._.]/ Adam Eivy85e774b10b62
diff --git a/server/services/loras.js b/server/services/loras.js
index 7f57036adf..949db0c2dc 100644
--- a/server/services/loras.js
+++ b/server/services/loras.js
@@ -30,6 +30,7 @@ import { verifySafetensorsStructure } from '../lib/hfCache.js';
import { isPlainObject } from '../lib/objects.js';
import { readCachedLoraEffectReport } from '../lib/loraEffect.js';
import { createKeyedFileWriteQueue } from '../lib/fileWriteQueue.js';
+import { createSingleFlight } from '../lib/singleFlight.js';
import {
applyDownloadToken,
baseModelToRunner,
@@ -67,10 +68,10 @@ 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();
+// One install attempt per requested Civitai model version at a time. A duplicate
+// submit shares the original result instead of racing it to the same destination
+// files, while two explicitly different versions remain independent.
+const civitaiInstalls = createSingleFlight();
const sidecarPath = (loraFilename) => join(PATHS.loras, `${loraFilename}${SIDECAR_SUFFIX}`);
@@ -597,15 +598,11 @@ const performCivitaiInstall = async (input, { modelId, versionId, fetchImpl }) =
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;
+ const installKey = `${modelId}:${versionId ?? 'latest'}`;
+ return civitaiInstalls.run(
+ installKey,
+ () => performCivitaiInstall(input, { modelId, versionId, fetchImpl }),
+ );
};
// Set of recognized LoRA families an HF import may target (image runners +
diff --git a/server/services/loras.test.js b/server/services/loras.test.js
index ee9f9bd78a..f348fbffb3 100644
--- a/server/services/loras.test.js
+++ b/server/services/loras.test.js
@@ -469,6 +469,56 @@ describe('installFromCivitai', () => {
expect(downloadCalls).toBe(1);
});
+ it('keeps concurrent installs for different versions of one model independent', async () => {
+ const downloadedBytes = validSafetensors();
+ const model = {
+ ...FAKE_MODEL,
+ modelVersions: [
+ ...FAKE_MODEL.modelVersions,
+ {
+ ...FAKE_MODEL.modelVersions[0],
+ id: 8,
+ files: [{
+ ...FAKE_MODEL.modelVersions[0].files[0],
+ downloadUrl: 'https://civitai.com/api/download/models/8',
+ }],
+ },
+ ],
+ };
+ let releaseMetadata;
+ const metadataGate = new Promise((resolve) => { releaseMetadata = resolve; });
+ let metadataCalls = 0;
+ let downloadCalls = 0;
+ const fetchImpl = async (url) => {
+ if (url.startsWith('https://civitai.com/api/v1/models/2600698')) {
+ metadataCalls += 1;
+ await metadataGate;
+ return mockJsonResponse(model);
+ }
+ if (url.startsWith('https://civitai.com/api/download/models/')) {
+ 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 version7 = lorasService.installFromCivitai({
+ url: 'https://civitai.com/models/2600698?modelVersionId=7',
+ }, { fetchImpl });
+ const version8 = lorasService.installFromCivitai({
+ url: 'https://civitai.com/models/2600698?modelVersionId=8',
+ }, { fetchImpl });
+
+ await vi.waitFor(() => expect(metadataCalls).toBe(2));
+ releaseMetadata();
+ const results = await Promise.all([version7, version8]);
+ expect(results.map((result) => result.civitai.versionId)).toEqual([7, 8]);
+ expect(downloadCalls).toBe(2);
+ });
+
it('releases the in-flight slot after a failed install so retry can proceed', async () => {
let metadataCalls = 0;
const fetchImpl = async () => {