From 3e99d3bb5af2306e8d1882a9de341275ec6b7ac4 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 2 Jul 2026 21:04:35 +0800 Subject: [PATCH] fix(webui): guard async polling against re-entrant overlap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 三个 setInterval 轮询(列表 reconcile、systemInfo 快刷、6h 更新检查)调度的都是 async tick,但 setInterval 不感知 Promise 完成——当某次请求慢于轮询间隔时,下一次 tick 会在 上一次未完成时又发起,导致同类请求并发堆叠、越积越慢。 每个 tick 加在途 running 互斥:上一次刷新还在飞就跳过本次,完成(try/finally,含出错)后 恢复调度。保留已有的 document.hidden 门控与变可见补刷。慢 tick 只是被跳过、完成后自愈, 不会永久停摆。 (引 non-overlapping-poll 工具 + 给 webui 新增测试基建对 3 个轮询点属过度工程,故就地加 互斥。) --- packages/webui/src/router/app-layout.tsx | 28 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/webui/src/router/app-layout.tsx b/packages/webui/src/router/app-layout.tsx index 4e741f99..65a8b1c6 100644 --- a/packages/webui/src/router/app-layout.tsx +++ b/packages/webui/src/router/app-layout.tsx @@ -140,12 +140,20 @@ export function AppLayout() { useEffect(() => { if (pollInterval <= 0) return; let cancelled = false; + let running = false; const reconcileMs = Math.max(pollInterval * 10, 10_000); const tick = async () => { // Skip while the tab is hidden — SSE already carries live updates, and a // backgrounded tab has no UI to refresh. Re-tick on becoming visible. - if (cancelled || document.hidden) return; - await Promise.all([refreshQqList(), refreshProcesses(), refreshConnections()]); + // `running` drops a tick while the previous refresh is still in flight, + // so a slow backend can't stack overlapping requests. + if (cancelled || document.hidden || running) return; + running = true; + try { + await Promise.all([refreshQqList(), refreshProcesses(), refreshConnections()]); + } finally { + running = false; + } }; // Initial tick primes the lists before the SSE handshake completes // (avoids a flash of empty state in the chrome). @@ -168,9 +176,11 @@ export function AppLayout() { useEffect(() => { if (pollInterval <= 0) return; let cancelled = false; + let running = false; const tick = async () => { - if (cancelled || document.hidden) return; - await refreshSystem(); + if (cancelled || document.hidden || running) return; + running = true; + try { await refreshSystem(); } finally { running = false; } }; tick(); const interval = setInterval(tick, pollInterval); @@ -187,8 +197,14 @@ export function AppLayout() { // list-polling above — GitHub's API is rate-limited, the result rarely // changes, and the server caches it anyway, so this is cheap. useEffect(() => { - refreshUpdate(); - const id = setInterval(() => refreshUpdate(), 6 * 60 * 60 * 1000); + let running = false; + const tick = async () => { + if (running) return; + running = true; + try { await refreshUpdate(); } finally { running = false; } + }; + tick(); + const id = setInterval(tick, 6 * 60 * 60 * 1000); return () => clearInterval(id); }, [refreshUpdate]);