fix(site): unsubscribe live metrics that resolve after leaving 1m (#2599)

pb.realtime.subscribe() resolves only once the server has confirmed the
subscription. When the chart time changed away from 1m before that, the
effect cleanup ran with the initial no-op, the subscription stayed active,
and its 1m points kept overwriting the 1h history. Track cancellation,
unsubscribe as soon as the late subscription resolves, and ignore messages
that arrive after cleanup. Fixes #2598.
This commit is contained in:
Mario Mohar
2026-10-12 09:14:31 -04:00
committed by GitHub
parent 596f96442e
commit da51dfd598
@@ -143,6 +143,11 @@ export function useSystemData(id: string) {
// subscribe to realtime metrics if chart time is 1m
useEffect(() => {
let unsub = () => {}
// subscribe() resolves only after the server confirms the subscription. If the
// effect is cleaned up before that (e.g. switching back to 1h quickly, or a slow
// realtime connection behind a proxy), unsub is still the no-op above, so the
// subscription would stay active and its 1m points would overwrite the history.
let cancelled = false
if (!system.id || chartTime !== "1m") {
return
}
@@ -156,6 +161,9 @@ export function useSystemData(id: string) {
.subscribe(
`rt_metrics`,
(data: { container: ContainerStatsRecord[]; info: SystemInfo; stats: SystemStats }) => {
if (cancelled) {
return
}
const now = Date.now()
const statsPoint = { created: now, stats: data.stats } as SystemStatsRecord
const containerPoint =
@@ -177,9 +185,14 @@ export function useSystemData(id: string) {
{ query: { system: system.id } }
)
.then((us) => {
if (cancelled) {
us()
return
}
unsub = us
})
return () => {
cancelled = true
unsub?.()
}
}, [chartTime, system.id])