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

test(loras): drain escaped sidecar writes before tearing down the temp root (#6265)

[._.]/ Adam Eivy29f33192cd17
diff --git a/server/services/loras.test.js b/server/services/loras.test.js
index 537d1f9173..ae0833604d 100644
--- a/server/services/loras.test.js
+++ b/server/services/loras.test.js
@@ -25,9 +25,33 @@ let lorasService;
let civitaiLib;
let atomicWriteHook;
+// Sidecar writes the service has enqueued but not yet finished (#6265).
+//
+// `listLoras()` heals a stale/absent `keyLayout` (and `fluxVariant`) by firing
+// `patchLoraSidecar(...).catch(() => {})` — deliberately fire-and-forget, so
+// listing never blocks or fails on a cache write. That promise outlives the
+// test that started it — two tests here were observed still holding a queued
+// write at teardown — so `afterEach` rm -rf's the temp root while a queued
+// read-modify-write is still walking it, and the next `beforeEach` calls
+// `vi.resetModules()` with that work in flight. Both races widen under load,
+// which matches #6265: the file passes on its own and read two just-written
+// sidecars as `{}` inside a contended full-suite shard.
+//
+// The queue itself is the only complete seam: every heal goes through
+// `createKeyedFileWriteQueue()` regardless of which `listLoras()` call site
+// started it, and the queued promise covers the whole stat → read → atomicWrite
+// cycle (hooking `atomicWrite` would miss a task that has not reached its write
+// yet). Wrapping the factory lets `afterEach` await exactly the work the test
+// started — a drain, not a sleep or a retry.
+const pendingSidecarWrites = new Set();
+const drainSidecarWrites = async () => {
+ while (pendingSidecarWrites.size) await Promise.all([...pendingSidecarWrites]);
+};
+
beforeEach(async () => {
tmpRoot = mkdtempSync(join(tmpdir(), 'portos-loras-test-'));
tmpLoras = join(tmpRoot, 'loras');
+ pendingSidecarWrites.clear();
vi.resetModules();
vi.doMock('../lib/fileUtils.js', async () => {
@@ -40,17 +64,50 @@ beforeEach(async () => {
: actual.atomicWrite(...args),
};
});
+ vi.doMock('../lib/fileWriteQueue.js', async () => {
+ const actual = await vi.importActual('../lib/fileWriteQueue.js');
+ return {
+ ...actual,
+ createKeyedFileWriteQueue: (...factoryArgs) => {
+ const queue = actual.createKeyedFileWriteQueue(...factoryArgs);
+ return (key, fn) => {
+ const write = queue(key, fn);
+ // Track a silenced copy: a rejected sidecar write is a real product
+ // outcome several tests assert on, and the drain must not re-throw it
+ // out of `afterEach`.
+ const tracked = write.catch(() => {});
+ pendingSidecarWrites.add(tracked);
+ tracked.finally(() => pendingSidecarWrites.delete(tracked));
+ return write; // callers still see the real resolve/reject
+ };
+ },
+ };
+ });
// Stub settings so resolveCivitaiKey doesn't read the real data/settings.json.
vi.doMock('./settings.js', () => ({
getSettings: async () => ({}),
}));
lorasService = await import('./loras.js');
civitaiLib = await import('../lib/civitai.js');
+
+ // Module identity at the read, asserted once per test. Every fixture below
+ // writes through the raw `tmpLoras` path while the service resolves its own
+ // `PATHS.loras` out of the mocked module — if those two ever diverge (a
+ // reused mock instance, a factory that did not re-run), the symptom is a
+ // silent empty read many tests later, because `readSidecar` collapses "wrong
+ // directory" into the same `null` it returns for "no sidecar". Fail here,
+ // naming both paths, instead.
+ const { PATHS } = await import('../lib/fileUtils.js');
+ expect(PATHS.loras).toBe(tmpLoras);
});
-afterEach(() => {
+afterEach(async () => {
+ // Before removing the tree the writes are aimed at, and before the next
+ // `beforeEach` resets the module graph out from under them.
+ await drainSidecarWrites();
atomicWriteHook = null;
vi.doUnmock('../lib/fileUtils.js');
+ vi.doUnmock('../lib/fileWriteQueue.js');
vi.doUnmock('./settings.js');
rmSync(tmpRoot, { recursive: true, force: true });
});
@@ -1228,11 +1285,11 @@ describe('LoRA key layout', () => {
});
const list = await lorasService.listLoras();
expect(list.find((l) => l.filename === 'lora-comfy.safetensors').keyLayout).toBe('comfyui');
- // Sidecar write is fire-and-forget; wait for it to complete.
- await vi.waitFor(async () => {
- const sidecar = JSON.parse(await fs.readFile(join(tmpLoras, 'lora-comfy.safetensors.metadata.json'), 'utf-8'));
- expect(sidecar.keyLayout).toBe('comfyui');
- });
+ // Sidecar write is fire-and-forget; drain the queue it was enqueued on
+ // rather than polling for the file to appear.
+ await drainSidecarWrites();
+ const sidecar = JSON.parse(await fs.readFile(join(tmpLoras, 'lora-comfy.safetensors.metadata.json'), 'utf-8'));
+ expect(sidecar.keyLayout).toBe('comfyui');
});
it('prefers the stored sidecar layout over re-reading the header', async () => {