From 2a9e01100bc34fe3c6dd9bc06e31c017cfb9f326 Mon Sep 17 00:00:00 2001 From: GOXF <131693427+GOXF@users.noreply.github.com> Date: Thu, 3 Dec 2026 08:42:20 +0000 Subject: [PATCH] =?UTF-8?q?fix(updater):=20=E4=BB=A3=E7=90=86=E6=B1=A0?= =?UTF-8?q?=E9=80=89=E8=8A=82=E7=82=B9=E5=8A=A0=204s=20=E8=B6=85=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A4=B1=E8=B4=A5=E7=9B=B4=E8=BF=9E=EF=BC=9B=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=E6=A3=80=E6=9F=A5=E4=B8=8E=E6=AF=8F=E6=9D=A1=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E7=BA=BF=E8=B7=AF=E3=80=81=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E5=86=99=20update.log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/goxfTimeout.test.ts | 14 +++++++++++++ src/main/goxfTimeout.ts | 26 ++++++++++++++++++++++++ src/main/goxfUpdater.ts | 38 ++++++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/main/goxfTimeout.test.ts create mode 100644 src/main/goxfTimeout.ts diff --git a/src/main/goxfTimeout.test.ts b/src/main/goxfTimeout.test.ts new file mode 100644 index 00000000..90956afa --- /dev/null +++ b/src/main/goxfTimeout.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from 'vitest' +import { withTimeout } from './goxfTimeout' + +describe('withTimeout', () => { + it('returns the value when it settles in time', async () => { + expect(await withTimeout(Promise.resolve(5), 50, 0)).toEqual({ value: 5, timedOut: false }) + }) + it('returns the fallback when the promise never settles', async () => { + expect(await withTimeout(new Promise(() => undefined), 20, 0)).toEqual({ value: 0, timedOut: true }) + }) + it('returns the fallback when the promise rejects', async () => { + expect(await withTimeout(Promise.reject(new Error('x')), 50, null)).toEqual({ value: null, timedOut: false }) + }) +}) diff --git a/src/main/goxfTimeout.ts b/src/main/goxfTimeout.ts new file mode 100644 index 00000000..e493d803 --- /dev/null +++ b/src/main/goxfTimeout.ts @@ -0,0 +1,26 @@ +/** GOXF: settle a promise within ms; on timeout resolve to `fallback` (the original keeps running, unobserved). */ +export function withTimeout(promise: Promise, ms: number, fallback: T): Promise<{ value: T; timedOut: boolean }> { + return new Promise((resolve) => { + let done = false + const timer = setTimeout(() => { + if (done) return + done = true + resolve({ value: fallback, timedOut: true }) + }, ms) + ;(timer as { unref?: () => void }).unref?.() + promise.then( + (value) => { + if (done) return + done = true + clearTimeout(timer) + resolve({ value, timedOut: false }) + }, + () => { + if (done) return + done = true + clearTimeout(timer) + resolve({ value: fallback, timedOut: false }) + } + ) + }) +} diff --git a/src/main/goxfUpdater.ts b/src/main/goxfUpdater.ts index 5fd8c9b8..6e8e59fa 100644 --- a/src/main/goxfUpdater.ts +++ b/src/main/goxfUpdater.ts @@ -42,6 +42,7 @@ import { type GoxfUpstreamPrompt } from '@shared/goxfUpdate' import { checkFailedMessage } from './updateCheck' +import { withTimeout } from './goxfTimeout' import { buildReplaceScript, decideResume, @@ -192,8 +193,18 @@ let updaterRules: string | undefined * tested entry) when it has one; a network failure there, or an empty pool, * falls back to the system network (which follows the system proxy). */ -async function updaterFetch(url: string, init: RequestInit): Promise { - const pick = await updaterPoolProxy().catch(() => null) +/** GOXF: picking a pool proxy can hang (keychain decrypt, Mihomo start); past this, go direct. */ +const POOL_PICK_TIMEOUT_MS = 4_000 +/** Update checks: each route (pool, then direct) gets its own budget, so a dead pool never eats the direct try. */ +const CHECK_ROUTE_TIMEOUT_MS = 12_000 + +/** routeTimeoutMs: per-route deadline when the caller has no signal of its own (downloads keep theirs). */ +async function updaterFetch(url: string, init: RequestInit, routeTimeoutMs?: number): Promise { + const routeInit = (): RequestInit => (init.signal || !routeTimeoutMs ? init : { ...init, signal: AbortSignal.timeout(routeTimeoutMs) }) + const name = basename(new URL(url).pathname) + const picked = await withTimeout(updaterPoolProxy(), POOL_PICK_TIMEOUT_MS, null) + if (picked.timedOut) logUpdate(`${name}: proxy pool did not answer in ${POOL_PICK_TIMEOUT_MS / 1000}s; using system network`) + const pick = picked.value if (pick) { try { const ses = session.fromPartition(UPDATER_PARTITION) @@ -202,15 +213,23 @@ async function updaterFetch(url: string, init: RequestInit): Promise { await ses.closeAllConnections().catch(() => undefined) updaterRules = pick.rules } - const res = await ses.fetch(url, init) - logUpdate(`${basename(new URL(url).pathname)} via proxy pool ${pick.label}`) + const res = await ses.fetch(url, routeInit()) + logUpdate(`${name} via proxy pool ${pick.label}: HTTP ${res.status}`) return res } catch (error) { + // A user pause/cancel (or the caller's own deadline) is not a reason to retry direct. if (init.signal?.aborted) throw error logUpdate(`proxy pool ${pick.label} failed (${error instanceof Error ? error.message : String(error)}); using system network`) } } - return net.fetch(url, init) + try { + const res = await net.fetch(url, routeInit()) + logUpdate(`${name} via system network: HTTP ${res.status}`) + return res + } catch (error) { + logUpdate(`${name} via system network failed (${error instanceof Error ? error.message : String(error)})`) + throw error + } } type Fetched = { ok: true; data: T | null } | { ok: false; message: string } @@ -218,9 +237,8 @@ type Fetched = { ok: true; data: T | null } | { ok: false; message: string } async function getJson(url: string): Promise> { try { const res = await updaterFetch(url, { - headers: { Accept: 'application/vnd.github+json', 'User-Agent': `BotArk/${currentVersion()}` }, - signal: AbortSignal.timeout(12_000) - }) + headers: { Accept: 'application/vnd.github+json', 'User-Agent': `BotArk/${currentVersion()}` } + }, CHECK_ROUTE_TIMEOUT_MS) // 404 = repo has no (latest) release yet: not an error. if (res.status === 404) return { ok: true, data: null } if (!res.ok) { @@ -255,6 +273,10 @@ async function check(manual: boolean): Promise { }) upstreamPrompt = decision.upstream portablePrompt = decision.portable + logUpdate( + `check (${manual ? 'manual' : 'auto'}, ${currentChannel()} channel): fork ${fork.ok ? 'ok' : `failed: ${fork.message}`}, ` + + `upstream ${up.ok ? 'ok' : 'failed'}; offer ${decision.portable?.version ?? 'none'}` + ) if (decision.upstream) broadcastGoxf({ type: 'upstream-available', prompt: decision.upstream }) if (decision.portable && !installing && !paused) { const { asset: _asset, delta: _delta, fallback: _fallback, ...prompt } = decision.portable