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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): abort the HF download when the SSE client disconnects
[._.]/ Adam Eivy453cf172c04f
diff --git a/server/routes/loras.js b/server/routes/loras.jsindex e62219f00e..a980602de3 100644--- a/server/routes/loras.js+++ b/server/routes/loras.js@@ -161,9 +161,15 @@ router.get('/install/huggingface/stream', asyncHandler(async (req, res) => {// must NOT bubble to the error middleware (it would try to re-set status on a// committed response). Catch and forward as an SSE `error` frame instead —// the sanctioned SSE-boundary exception to the no-try/catch rule.+ // On client disconnect (tab close, navigation, dropped connection) actually+ // ABORT the download via the controller — not just suppress frames — so a+ // multi-GB LoRA transfer doesn't keep running unwatched and a retry can't race+ // a still-in-flight install.+ const controller = new AbortController();let aborted = false;- req.on('close', () => { aborted = true; });+ req.on('close', () => { aborted = true; controller.abort(); });await installFromHuggingface(data, {+ signal: controller.signal,onProgress: ({ received, total }) => {if (aborted) return;send({ type: 'progress', received, total, progress: total > 0 ? received / total : null });diff --git a/server/services/loras.js b/server/services/loras.jsindex 2c3d4586d5..4f1de4d7c5 100644--- a/server/services/loras.js+++ b/server/services/loras.js@@ -262,9 +262,15 @@ export const resolveCivitaiKey = async () => {// as SSE `progress` frames so the UI can show a percentage. `total` is 0 when// the response carries no Content-Length (chunked / some CDN redirects), in// which case the client renders an indeterminate bar.-const downloadToFile = async (url, destPath, { fetchImpl = fetch, headers = {} , hasApiKey = false, source = 'civitai', onProgress = null } = {}) => {+const downloadToFile = async (url, destPath, { fetchImpl = fetch, headers = {} , hasApiKey = false, source = 'civitai', onProgress = null, signal = null } = {}) => {const tmpPath = `${destPath}.${randomBytes(6).toString('hex')}.partial`;- const res = await fetchImpl(url, { headers, redirect: 'follow' });+ // `signal` (optional) aborts the fetch + stream mid-download — the streaming+ // install route passes it so an SSE client disconnect actually cancels a+ // multi-GB transfer instead of letting it run to completion unwatched. On+ // abort the fetch rejects (or the body stream errors), the pipeline().catch+ // below unlinks the .partial, and the throw propagates before any sidecar is+ // written — so a cancelled install leaves nothing behind.+ const res = await fetchImpl(url, { headers, redirect: 'follow', signal });if (!res.ok) {if (res.status === 401 || res.status === 403) {// HuggingFace LoRAs: a 401/403 means the repo is gated and the token is@@ -446,7 +452,7 @@ const VIDEO_LORA_FAMILY_VALUES = new Set(Object.values(VIDEO_LORA_FAMILIES));// sidecar. The family is auto-detected from the repo id / tags / base_model,// or taken from an explicit `input.family` override (validated against the// known video families). Returns the new sidecar JSON.-export const installFromHuggingface = async (input, { fetchImpl = fetch, onProgress = null } = {}) => {+export const installFromHuggingface = async (input, { fetchImpl = fetch, onProgress = null, signal = null } = {}) => {const { repo, revision } = parseHuggingfaceLoraRef(input?.url);// Stored/env/CLI HF token — only needed for gated repos, but harmless to// send on public ones (HF ignores a bearer it doesn't require).@@ -498,6 +504,7 @@ export const installFromHuggingface = async (input, { fetchImpl = fetch, onProgrhasApiKey: !!token,source: 'huggingface',onProgress,+ signal,});const sidecar = buildHfLoraSidecar({ repo, revision, file, model, family, filename });diff --git a/server/services/loras.test.js b/server/services/loras.test.jsindex 5e41ed488c..4b2134fc7c 100644--- a/server/services/loras.test.js+++ b/server/services/loras.test.js@@ -505,6 +505,27 @@ describe('installFromHuggingface', () => {expect(ticks[ticks.length - 1]).toEqual({ received: 7, total: 0 });});+ it('forwards an AbortSignal to the download fetch so an SSE disconnect can cancel it', async () => {+ const controller = new AbortController();+ let sawSignal;+ const fetchImpl = async (url, opts) => {+ if (url.startsWith('https://huggingface.co/api/models/')) return mockJsonResponse(HF_MODEL);+ if (url.includes('/resolve/main/pytorch_lora_weights.safetensors')) {+ sawSignal = opts?.signal;+ const stream = new ReadableStream({ start(c) { c.enqueue(new Uint8Array(Buffer.from('w'))); c.close(); } });+ return { ok: true, status: 200, body: stream };+ }+ throw new Error(`unexpected fetch: ${url}`);+ };+ await lorasService.installFromHuggingface(+ { url: 'https://huggingface.co/fal/ltx2.3-audio-reactive-lora', token: 'hf_test' },+ { fetchImpl, signal: controller.signal },+ );+ // The controller's signal must reach the actual weights download (not just+ // the metadata fetch) — that's the transfer a disconnect needs to cancel.+ expect(sawSignal).toBe(controller.signal);+ });+it('accepts an explicit family override', async () => {const fetchImpl = async (url) => {if (url.startsWith('https://huggingface.co/api/models/')) {