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
fix: abort a stalled Ollama.app upgrade download instead of hanging (#11682)
[._.]/ Adam Eivy624887fd679a
diff --git a/server/routes/localLlm.js b/server/routes/localLlm.jsindex 808b85aaa2..02f1c70d11 100644--- a/server/routes/localLlm.js+++ b/server/routes/localLlm.js@@ -572,7 +572,10 @@ router.post('/install', asyncHandler(async (req, res) => {router.post('/upgrade-backend', asyncHandler(async (req, res) => {const { backend } = validateRequest(localLlmInstallBackendSchema, req.body)const emit = emitter(req)- const result = await upgradeBackend(backend, ({ event, message }) => emit(event, message))+ // Abort an in-flight download when the client goes away before we respond.+ const ac = new AbortController()+ res.on('close', () => { if (!res.writableFinished) ac.abort() })+ const result = await upgradeBackend(backend, ({ event, message }) => emit(event, message), ac.signal).catch((err) => {emit('error', `Upgrade failed: ${err.message}`)throw errdiff --git a/server/services/localLlm.js b/server/services/localLlm.jsindex 7ac5bd2954..a5c9b82d12 100644--- a/server/services/localLlm.js+++ b/server/services/localLlm.js@@ -33,7 +33,7 @@ import { join } from 'path'import { tmpdir } from 'os'import { fileURLToPath } from 'url'import { pipeline } from 'stream/promises'-import { Readable } from 'stream'+import { Readable, Transform } from 'stream'import { ensureDir, pathExists, sleep } from '../lib/fileUtils.js'import { readPortosEnvValue, upsertPortosEnvLine } from '../lib/portosEnv.js'import { ServerError } from '../lib/errorHandler.js'@@ -369,7 +369,47 @@ async function waitForOllamaVersion(timeoutMs = 30_000) {* version advanced, because a second install ahead of the bundle on PATH can* keep serving the old binary past a perfectly successful swap.*/-async function upgradeOllamaMacApp(emit) {+const DOWNLOAD_HEADER_TIMEOUT_MS = 30_000+const DOWNLOAD_IDLE_TIMEOUT_MS = 60_000++/**+ * Download `url` to `destPath` with stall detection instead of a total+ * deadline (which would abort slow-but-progressing links): the response headers+ * must arrive within `headerMs`, then every body chunk resets an idle timer that+ * aborts after `idleMs` without bytes. `signal` aborts early (client gone).+ * Resolves `{ error }` on any failure, `{}` on success — never throws.+ */+export async function downloadWithStallGuard(url, destPath, signal, { headerMs = DOWNLOAD_HEADER_TIMEOUT_MS, idleMs = DOWNLOAD_IDLE_TIMEOUT_MS } = {}) {+ const ac = new AbortController()+ let stall = null+ let timer = null+ const arm = (ms, message) => {+ clearTimeout(timer)+ timer = setTimeout(() => { stall = message; ac.abort() }, ms)+ }+ const onExternalAbort = () => ac.abort()+ if (signal?.aborted) return { error: 'Download cancelled.' }+ signal?.addEventListener('abort', onExternalAbort, { once: true })+ try {+ arm(headerMs, `Download stalled — no response from the server within ${Math.round(headerMs / 1000)} s. Check the connection and retry.`)+ const dl = await fetch(url, { signal: ac.signal })+ if (!dl?.ok || !dl.body) return { error: `Download failed: ${dl?.statusText || 'no response body'}` }+ const idleMessage = `Download stalled — no data for ${Math.round(idleMs / 1000)} s. Check the connection and retry.`+ arm(idleMs, idleMessage)+ const touch = new Transform({ transform(chunk, _enc, cb) { arm(idleMs, idleMessage); cb(null, chunk) } })+ await pipeline(Readable.fromWeb(dl.body), touch, createWriteStream(destPath), { signal: ac.signal })+ return {}+ } catch (err) {+ if (stall) return { error: stall }+ if (signal?.aborted) return { error: 'Download cancelled.' }+ return { error: `Download failed: ${err.message}` }+ } finally {+ clearTimeout(timer)+ signal?.removeEventListener('abort', onExternalAbort)+ }+}++async function upgradeOllamaMacApp(emit, signal) {const appPath = '/Applications/Ollama.app'emit('Looking up the latest Ollama release on GitHub…')@@ -394,18 +434,10 @@ async function upgradeOllamaMacApp(emit) {await ensureDir(tmpDir)emit(`Downloading Ollama ${release.tag_name} (${Math.round(asset.size / 1024 / 1024)} MB)…`)- // No timeout signal here: it would cover the entire multi-hundred-MB body- // stream, turning slow links into mid-download aborts. User-triggered and- // recoverable, so an unbounded stream is the lesser evil.- const dl = await fetch(asset.browser_download_url).catch((err) => ({ _err: err.message }))- if (dl._err || !dl?.ok || !dl.body) {- await rm(tmpDir, { recursive: true, force: true }).catch(() => {})- return { success: false, error: `Download failed: ${dl?._err || dl?.statusText || 'no response body'}` }- }- const pipeErr = await pipeline(Readable.fromWeb(dl.body), createWriteStream(zipPath)).then(() => null, (err) => err)- if (pipeErr) {+ const dl = await downloadWithStallGuard(asset.browser_download_url, zipPath, signal)+ if (dl.error) {await rm(tmpDir, { recursive: true, force: true }).catch(() => {})- return { success: false, error: `Download failed: ${pipeErr.message}` }+ return { success: false, error: dl.error }}emit('Stopping running Ollama…')@@ -520,8 +552,9 @@ async function upgradeOllamaMacApp(emit) {** @param {string} backend - 'ollama' | 'lmstudio'* @param {(p: { event: string, message: string }) => void} [onProgress]+ * @param {AbortSignal} [signal] - aborts an in-flight Ollama.app download (client disconnect)*/-export async function upgradeBackend(backend, onProgress = () => {}) {+export async function upgradeBackend(backend, onProgress = () => {}, signal) {if (!isBackend(backend)) return { success: false, error: `Unknown backend: ${backend}` }const emit = (message) => onProgress({ event: 'start', message })const label = backend === 'ollama' ? 'Ollama' : 'LM Studio'@@ -546,7 +579,7 @@ export async function upgradeBackend(backend, onProgress = () => {}) {// macOS Ollama with a .app present — direct download is the only path that// actually replaces the binary that's serving requests.if (backend === 'ollama' && await pathExists(macAppPath('ollama'))) {- return upgradeOllamaMacApp(emit)+ return upgradeOllamaMacApp(emit, signal)}// Everything else: route through Homebrew.diff --git a/server/services/localLlm.test.js b/server/services/localLlm.test.jsindex fbff255a8c..072ae83491 100644--- a/server/services/localLlm.test.js+++ b/server/services/localLlm.test.js@@ -856,6 +856,52 @@ describe('localLlm', () => {// ran. Before #7238 the old bundle was `rm`'d first and its failure swallowed,// which could leave the machine with no Ollama at all, or a nested// Ollama.app/Ollama.app reported as a successful upgrade.+ describe('downloadWithStallGuard', () => {+ const dest = () => path.join(os.tmpdir(), `portos-stall-test-${Date.now()}-${Math.random().toString(36).slice(2)}.zip`);+ afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); });++ it('aborts with the stall error when the body stops emitting', async () => {+ vi.useFakeTimers();+ vi.stubGlobal('fetch', vi.fn(async (_url, { signal }) => ({+ ok: true,+ body: new ReadableStream({+ start(c) { c.enqueue(new Uint8Array([1])); signal.addEventListener('abort', () => c.error(new Error('aborted'))); }+ })+ })));+ const p = svc.downloadWithStallGuard('https://example.com/x.zip', dest());+ await vi.advanceTimersByTimeAsync(60_001);+ expect(await p).toEqual({ error: expect.stringMatching(/no data for 60 s/) });+ });++ it('does not abort a slow stream that keeps making progress', async () => {+ vi.useFakeTimers();+ let n = 0;+ vi.stubGlobal('fetch', vi.fn(async () => ({+ ok: true,+ body: new ReadableStream({+ async pull(c) {+ if (n++ >= 4) return c.close();+ await new Promise((r) => setTimeout(r, 40_000));+ c.enqueue(new Uint8Array([n]));+ }+ })+ })));+ const out = dest();+ const p = svc.downloadWithStallGuard('https://example.com/x.zip', out);+ await vi.advanceTimersByTimeAsync(200_000);+ expect(await p).toEqual({});+ fs.rmSync(out, { force: true });+ });++ it('aborts when no response headers arrive within the header deadline', async () => {+ vi.useFakeTimers();+ vi.stubGlobal('fetch', vi.fn((_url, { signal }) => new Promise((_res, rej) => signal.addEventListener('abort', () => rej(new Error('aborted'))))));+ const p = svc.downloadWithStallGuard('https://example.com/x.zip', dest());+ await vi.advanceTimersByTimeAsync(30_001);+ expect(await p).toEqual({ error: expect.stringMatching(/no response from the server within 30 s/) });+ });+ });+describe('upgradeBackend — macOS Ollama.app swap', () => {const APP = '/Applications/Ollama.app';const aside = () => [...appFs.entries.keys()].filter((p) => p.startsWith(`${APP}.portos-old-`));