From 14cee0d6264e3c651c97aec36c398fec4ba59c42 Mon Sep 17 00:00:00 2001 From: vkpdeveloper Date: Sun, 29 Nov 2026 18:24:12 +0530 Subject: [PATCH 1/3] fix: the dashboard reconnects on its own after VM Bridge restarts While VM Bridge restarts, the tunnel answers the event stream with a 502, and the browser never retries a stream after an error answer. The dashboard now opens a new stream after 3, 5, 10, 15, then every 30 seconds, and reloads the request log once it is back. --- apps/web/src/api.ts | 61 ++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/apps/web/src/api.ts b/apps/web/src/api.ts index 7281ef56..64eea2fd 100644 --- a/apps/web/src/api.ts +++ b/apps/web/src/api.ts @@ -34,6 +34,9 @@ export async function api(path: string, options: { method?: string; body?: un const trafficLimit = 300; +/** How long to wait before opening the event stream again after each failure in a row. */ +const retryDelaysMs = [3_000, 5_000, 10_000, 15_000, 30_000]; + /** The latest `trafficLimit` requests and as many other events, so a burst of requests cannot hide startup or a failure. */ function trimTraffic(events: TrafficEvent[]) { let requests = 0; @@ -74,30 +77,48 @@ export function useLive() { loadTraffic(true); loadOverview(); }, 5_000); - const source = new EventSource("/admin/api/events"); - source.onopen = () => setOnline(true); - source.onerror = () => { - setOnline(false); - // A closed stream after a failed reconnect usually means the session ended, such as after a password change elsewhere. - if (source.readyState !== EventSource.CLOSED) return; - void api<{ authenticated: boolean }>("/auth").then( - (auth) => { - if (!auth.authenticated) for (const listener of loggedOut) listener(); - }, - () => {}, - ); - }; - source.onmessage = (event) => { - streaming = true; - clearInterval(polling); - const live = JSON.parse(event.data as string) as LiveMessage; - if (live.type === "overview") setOverview(live.overview); - else setTraffic((current) => trimTraffic([...current, live.event])); + let source: EventSource | undefined; + let retry: ReturnType | undefined; + let failures = 0; + let opens = 0; + const connect = () => { + const stream = new EventSource("/admin/api/events"); + source = stream; + stream.onopen = () => { + failures = 0; + setOnline(true); + // The stream starts with the overview. Requests made while it was down come from the traffic list. + if (opens++ > 0) loadTraffic(false); + }; + stream.onerror = () => { + setOnline(false); + // The browser retries a dropped connection by itself, but gives up for good on an error answer, such as + // the tunnel's while VM Bridge restarts. Then open a new stream, waiting longer after each failure. + if (stream.readyState !== EventSource.CLOSED) return; + const delay = retryDelaysMs[Math.min(failures++, retryDelaysMs.length - 1)]; + const reconnect = () => { + if (!cancelled) retry = setTimeout(connect, delay); + }; + // It can also mean the session ended, such as after a password change elsewhere. + void api<{ authenticated: boolean }>("/auth").then((auth) => { + if (auth.authenticated) reconnect(); + else for (const listener of loggedOut) listener(); + }, reconnect); + }; + stream.onmessage = (event) => { + streaming = true; + clearInterval(polling); + const live = JSON.parse(event.data as string) as LiveMessage; + if (live.type === "overview") setOverview(live.overview); + else setTraffic((current) => trimTraffic([...current, live.event])); + }; }; + connect(); return () => { cancelled = true; clearInterval(polling); - source.close(); + clearTimeout(retry); + source?.close(); }; }, []); From 5fc1164ad70944352743e91d86c6df4ad83f6ca3 Mon Sep 17 00:00:00 2001 From: vkpdeveloper Date: Mon, 30 Nov 2026 18:46:58 +0530 Subject: [PATCH 2/3] fix: the dashboard reconnects even when the session check hangs, and keeps requests made while it reloads --- apps/web/src/api.ts | 54 +++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/apps/web/src/api.ts b/apps/web/src/api.ts index 64eea2fd..43bda6be 100644 --- a/apps/web/src/api.ts +++ b/apps/web/src/api.ts @@ -68,8 +68,27 @@ export function useLive() { let cancelled = false; let streaming = false; // Cloudflare's quick tunnels hold event streams back, so until the stream delivers, read the same data every few seconds. - const loadTraffic = (onlyWhileWaiting: boolean) => - void api("/traffic").then((recent) => !cancelled && !(onlyWhileWaiting && streaming) && setTraffic(trimTraffic(recent)), () => {}); + // Stream events that arrive while the traffic list loads, which the list may have been read before. + let arrived: TrafficEvent[] | undefined; + let loads = 0; + const loadTraffic = (onlyWhileWaiting: boolean) => { + const load = ++loads; + arrived = []; + void api("/traffic").then( + (recent) => { + // Only the latest load counts: an earlier one read an older list. + if (cancelled || load !== loads) return; + const during = arrived ?? []; + arrived = undefined; + if (onlyWhileWaiting && streaming) return; + const last = recent.at(-1)?.id ?? 0; + setTraffic(trimTraffic([...recent, ...during.filter((event) => event.id > last)])); + }, + () => { + if (load === loads) arrived = undefined; + }, + ); + }; const loadOverview = () => void api("/overview").then((next) => !cancelled && !streaming && setOverview(next), () => {}); loadTraffic(false); loadOverview(); @@ -85,10 +104,11 @@ export function useLive() { const stream = new EventSource("/admin/api/events"); source = stream; stream.onopen = () => { - failures = 0; setOnline(true); - // The stream starts with the overview. Requests made while it was down come from the traffic list. - if (opens++ > 0) loadTraffic(false); + // The stream starts with the overview. Requests made while it was down, or before a first stream that took + // several tries, come from the traffic list. + if (opens++ > 0 || failures > 0) loadTraffic(false); + failures = 0; }; stream.onerror = () => { setOnline(false); @@ -96,21 +116,27 @@ export function useLive() { // the tunnel's while VM Bridge restarts. Then open a new stream, waiting longer after each failure. if (stream.readyState !== EventSource.CLOSED) return; const delay = retryDelaysMs[Math.min(failures++, retryDelaysMs.length - 1)]; - const reconnect = () => { - if (!cancelled) retry = setTimeout(connect, delay); - }; - // It can also mean the session ended, such as after a password change elsewhere. - void api<{ authenticated: boolean }>("/auth").then((auth) => { - if (auth.authenticated) reconnect(); - else for (const listener of loggedOut) listener(); - }, reconnect); + if (!cancelled) retry = setTimeout(connect, delay); + // It can also mean the session ended, such as after a password change elsewhere. Checked beside the retry, so + // a check that never answers cannot hold the retry up. + void api<{ authenticated: boolean }>("/auth").then( + (auth) => { + if (auth.authenticated) return; + clearTimeout(retry); + for (const listener of loggedOut) listener(); + }, + () => {}, + ); }; stream.onmessage = (event) => { streaming = true; clearInterval(polling); const live = JSON.parse(event.data as string) as LiveMessage; if (live.type === "overview") setOverview(live.overview); - else setTraffic((current) => trimTraffic([...current, live.event])); + else { + arrived?.push(live.event); + setTraffic((current) => trimTraffic([...current, live.event])); + } }; }; connect(); From a7c43fb4c54bc7c4118914054d93360af9331b9f Mon Sep 17 00:00:00 2001 From: vkpdeveloper Date: Mon, 30 Nov 2026 18:57:59 +0530 Subject: [PATCH 3/3] fix: a slow traffic list load still counts while the dashboard polls --- apps/web/src/api.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/web/src/api.ts b/apps/web/src/api.ts index 43bda6be..908222bd 100644 --- a/apps/web/src/api.ts +++ b/apps/web/src/api.ts @@ -67,11 +67,14 @@ export function useLive() { useEffect(() => { let cancelled = false; let streaming = false; - // Cloudflare's quick tunnels hold event streams back, so until the stream delivers, read the same data every few seconds. - // Stream events that arrive while the traffic list loads, which the list may have been read before. + // Stream events that arrive while the traffic list loads, which the list may have been read before. Set only while + // a load is under way. let arrived: TrafficEvent[] | undefined; let loads = 0; const loadTraffic = (onlyWhileWaiting: boolean) => { + // A poll leaves a load still under way to finish. Otherwise polls every 5 seconds would each replace a slower one, + // and none would ever count. + if (onlyWhileWaiting && arrived) return; const load = ++loads; arrived = []; void api("/traffic").then( @@ -92,6 +95,7 @@ export function useLive() { const loadOverview = () => void api("/overview").then((next) => !cancelled && !streaming && setOverview(next), () => {}); loadTraffic(false); loadOverview(); + // Cloudflare's quick tunnels hold event streams back, so until the stream delivers, read the same data every few seconds. const polling = setInterval(() => { loadTraffic(true); loadOverview(); @@ -134,7 +138,8 @@ export function useLive() { const live = JSON.parse(event.data as string) as LiveMessage; if (live.type === "overview") setOverview(live.overview); else { - arrived?.push(live.event); + // Bounded like the list itself, in case the load takes a long time. + if (arrived) arrived = trimTraffic([...arrived, live.event]); setTraffic((current) => trimTraffic([...current, live.event])); } };