From 84cdd99868db677894625f889502216e88d9914a Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 24 Jun 2026 23:05:25 +0800 Subject: [PATCH] fix(webui): dropped-frame reconcile + split systemInfo cadence + diff-loop unref MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three findings from the adversarial review, all in the WebUI integration: D: SSE {kind:'dropped'} now triggers a one-shot REST refresh of qq-list/processes/connections instead of being silently ignored. Reasoning: when createFramePusher drops frames under backpressure, the NEXT delivered frame is per-resource (only the resource whose debounce timer next fires) — NOT a refresh of all 3. If the dropped frame was the only update for a slow-changing resource (e.g. connections stable afterwards), the WebUI stays stale until the 30s reconcile. The refresh is fire-and-forget; each refresh helper already swallows errors. E: systemInfo gets its own fast useEffect on the user's pollInterval (default 3s). The earlier commit slowed it to 30s claiming the data was 'daily cadence', but SystemInfo carries live values (uptime, cpu.loadAvg, memory.usagePercent, runtime.heapUsed) that the overview widget animates — 30s is a visible regression. The other 3 resources stay on the 30s SSE-reconcile fallback. F: connection-diff-loop's setInterval is now .unref()'d (mirrors the tokenJanitor pattern at server.ts:462). The discarded handle is now commented as intentional — initWebUI has no shutdown path, so the handle has no consumer; unref is the right cleanup primitive. WebUI typecheck clean. 437/437 core suite (no behavior change in core tests — only the diff-loop file's timer.unref?.() call is new and is optional-chained so vitest's fake timers don't crash). --- .../core/src/webui/connection-diff-loop.ts | 5 ++ packages/core/src/webui/server.ts | 6 +- packages/webui/src/router/app-layout.tsx | 59 ++++++++++++++----- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/packages/core/src/webui/connection-diff-loop.ts b/packages/core/src/webui/connection-diff-loop.ts index d5542147..17d01e49 100644 --- a/packages/core/src/webui/connection-diff-loop.ts +++ b/packages/core/src/webui/connection-diff-loop.ts @@ -74,6 +74,11 @@ export function startConnectionDiffLoop(opts: ConnectionDiffLoopOptions): Connec }; const timer = setInterval(tick, intervalMs); + // Don't pin the Node event loop: the loop is permanent for the server + // lifetime by design, but tests / a future hot-reload scenario must be + // able to exit cleanly without an outstanding interval ref. Mirrors the + // tokenJanitor pattern in server.ts. + timer.unref?.(); return { dispose(): void { diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 3fc19eb2..6742048e 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -334,7 +334,9 @@ export async function initWebUI( // Connection-status diff loop: OneBot adapters don't emit events for // listening/connected/client-count changes, so we poll the snapshot at // 500ms and publish 'connections' to the StateBus when it actually - // moves. One loop per server lifetime, lives until process exit. + // moves. One loop per server lifetime, lives until process exit (the + // setInterval is .unref'd inside the loop so it doesn't pin the event + // loop on shutdown). // // `pickComparable` strips `adapter.detail` — that's a localised human // string assembled with HH:MM:SS timestamps from the last webhook @@ -344,6 +346,8 @@ export async function initWebUI( // renders differently. The SSE handler still ships the FULL snapshot // (with `detail`) — it's only the diff key that's pruned. if (listener.stateBus) { + // Handle deliberately discarded — initWebUI has no shutdown path; the + // loop's setInterval is .unref'd so it doesn't pin the event loop. startConnectionDiffLoop({ bus: listener.stateBus, getSnapshot: () => oneBotManager.getConnectionStatuses(), diff --git a/packages/webui/src/router/app-layout.tsx b/packages/webui/src/router/app-layout.tsx index 777b451b..15e314e0 100644 --- a/packages/webui/src/router/app-layout.tsx +++ b/packages/webui/src/router/app-layout.tsx @@ -106,9 +106,13 @@ export function AppLayout() { // Primary live-state path: subscribe to /api/state/stream and apply // pushed snapshots directly. Initial frames on connect prime - // processes/qqList/connections without a polling tick. systemInfo isn't - // covered by the SSE feed (it changes on a daily cadence — kernel/ - // distro details) so it stays REST-driven below. + // processes/qqList/connections without a polling tick. + // + // On `{kind:'dropped'}` (backpressure made the server skip some frames), + // a per-resource SSE auto-recovery is NOT guaranteed: the next delivered + // frame is whatever resource next changes, not necessarily the one whose + // update we lost. So on dropped, kick a one-shot REST reconcile to + // recover immediately instead of waiting up to 30s for the fallback. useEffect(() => { const dispose = api.stateStream({ onEvent: (event) => { @@ -116,37 +120,60 @@ export function AppLayout() { if (event.resource === 'processes') setProcessList(event.data); else if (event.resource === 'qq-list') setQqList(event.data); else if (event.resource === 'connections') setConnections(event.data); + return; + } + if ('kind' in event && event.kind === 'dropped') { + // Fire-and-forget; each refresh has its own try/catch. + void refreshQqList(); + void refreshProcesses(); + void refreshConnections(); } - // 'ready' / 'dropped' control frames intentionally ignored — a - // 'dropped' just means the SSE skipped some intermediate frames - // under backpressure; the NEXT delivered frame is a full snapshot - // so the UI auto-recovers. }, }); return () => { dispose(); }; - }, [api]); + }, [api, refreshQqList, refreshProcesses, refreshConnections]); - // Fallback reconcile loop: even with the SSE primary, fire a single - // REST tick every `pollInterval` × 10 (so the default 3s → 30s) just to - // recover from a silent SSE drop (proxy rewrites text/event-stream, - // browser tab thrashing, etc.). systemInfo refreshes here too. + // Slow reconcile fallback for the SSE-covered resources. SSE drops can + // be silent (proxy rewrites text/event-stream, tab thrashing). One REST + // tick per (pollInterval × 10, min 10s) recovers from those without + // hammering the server. useEffect(() => { if (pollInterval <= 0) return; let cancelled = false; const reconcileMs = Math.max(pollInterval * 10, 10_000); const tick = async () => { if (cancelled) return; - await Promise.all([refreshQqList(), refreshProcesses(), refreshSystem(), refreshConnections()]); + await Promise.all([refreshQqList(), refreshProcesses(), refreshConnections()]); }; - // Initial tick so the four lists exist before the SSE handshake - // completes (avoids a flash of empty state in the chrome). + // Initial tick primes the lists before the SSE handshake completes + // (avoids a flash of empty state in the chrome). tick(); const interval = setInterval(tick, reconcileMs); return () => { cancelled = true; clearInterval(interval); }; - }, [pollInterval, refreshQqList, refreshProcesses, refreshSystem, refreshConnections]); + }, [pollInterval, refreshQqList, refreshProcesses, refreshConnections]); + + // systemInfo has its OWN fast cadence — it carries live values (uptime, + // cpu loadAvg, memory.usagePercent, runtime.heapUsed) that the overview + // widget visibly animates. It isn't on the SSE feed, so a 30s reconcile + // would visibly lag the dashboard. Keep the user's configured + // pollInterval (default 3s) here. + useEffect(() => { + if (pollInterval <= 0) return; + let cancelled = false; + const tick = async () => { + if (cancelled) return; + await refreshSystem(); + }; + tick(); + const interval = setInterval(tick, pollInterval); + return () => { + cancelled = true; + clearInterval(interval); + }; + }, [pollInterval, refreshSystem]); // Update check runs on its own slow cadence (6h), independent of the fast // list-polling above — GitHub's API is rate-limited, the result rarely