From fca5ca78de81666be249769a8496d4f0567cc022 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Wed, 4 Nov 2026 19:04:29 +0700 Subject: [PATCH] fix(dashboard): treat an open log tail as live before its first line The tail counted as live, cleared its error and reset its backoff only when a line arrived. A quiet application therefore showed "Connecting" forever, a 429 warning stuck after the retry succeeded, and every idle close by the proxy (its tail idle timeout) raised the backoff towards 30s and reconnected without since, losing lines written in the gap. The tail now reports when the response opens. That marks it live, clears the error and resets the backoff; a clean close reconnects at the base delay; and with no line seen yet the reconnect resumes from the time the stream opened. Assisted-by: LLM Signed-off-by: ZverGuy --- .../src/lib/observability/proxy.test.ts | 18 +++++++++ .../console/src/lib/observability/proxy.ts | 2 + .../lib/observability/use-log-tail.test.tsx | 37 +++++++++++++++++++ .../src/lib/observability/use-log-tail.ts | 19 +++++++--- 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts index 69a8c7c8c..2809905d8 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts @@ -194,6 +194,24 @@ describe("tailLogs", () => { expect(err).toMatchObject({ kind: "forbidden" }) }) + + it("says the stream opened before any line arrives", async () => { + const { client } = clientReturning(new Response("")) + const onOpen = vi.fn() + + await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen) + + expect(onOpen).toHaveBeenCalledTimes(1) + }) + + it("does not say the stream opened when the tail is refused", async () => { + const { client } = clientReturning(new Response("", { status: 429 })) + const onOpen = vi.fn() + + await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen).catch(() => {}) + + expect(onOpen).not.toHaveBeenCalled() + }) }) describe("isScopeWide", () => { diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts index c9fce3d4f..3b5567f6b 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts @@ -202,8 +202,10 @@ export async function tailLogs( params: { query: string; since?: string }, onEntry: (entry: LogEntry) => void, signal?: AbortSignal, + onOpen?: () => void, ): Promise { const res = await proxyGet(client, `${root}select/logsql/tail`, params, signal) + onOpen?.() if (!res.body) return const reader = res.body.getReader() const decoder = new TextDecoder() diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.test.tsx b/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.test.tsx index 0c2306613..b5232d18e 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.test.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.test.tsx @@ -10,6 +10,10 @@ function ndjson(...lines: string[]): Response { return new Response(lines.map((l) => `${l}\n`).join("")) } +function openStream(): Response { + return new Response(new ReadableStream({ start() {} })) +} + function setup(...responses: Response[]) { const client = new K8sClient() const raw = vi.spyOn(client, "raw") @@ -75,6 +79,39 @@ describe("useLogTail", () => { await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["a"])) }) + it("goes live and clears a load warning once the stream opens, before any line", async () => { + const { wrapper } = setup(new Response("too many concurrent requests", { status: 429 }), openStream()) + + const { result } = renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 1 }), { wrapper }) + + await waitFor(() => expect(result.current.state).toBe("live")) + expect(result.current.error).toBeNull() + }) + + it("reconnects after an idle close at the base delay, from when the quiet stream opened", async () => { + const delays: number[] = [] + const realSetTimeout = globalThis.setTimeout + const spy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void, ms?: number) => { + if (ms !== undefined && ms % 7 === 0 && ms > 0) delays.push(ms) + return realSetTimeout(fn, ms) + }) as typeof setTimeout) + const refused = () => new Response("", { status: 429 }) + const { raw, wrapper } = setup(refused(), refused(), ndjson()) + + try { + renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 7 }), { wrapper }) + await waitFor(() => expect(raw).toHaveBeenCalledTimes(4)) + } finally { + spy.mockRestore() + } + + expect(delays).toEqual([7, 14, 7]) + expect(new URL(raw.mock.calls[2][0], "http://x").searchParams.has("since")).toBe(false) + const since = new URL(raw.mock.calls[3][0], "http://x").searchParams.get("since") + expect(since).not.toBeNull() + expect(new Date(since!).toISOString()).toBe(since) + }) + it("does not connect while disabled", () => { const { raw, wrapper } = setup() diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.ts b/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.ts index 53cbaf856..2e3b4d706 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.ts +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/use-log-tail.ts @@ -66,15 +66,22 @@ export function useLogTail( } const run = async () => { - for (let attempt = 0; !signal.aborted; attempt++) { + let failures = 0 + let openedAt: string | undefined + while (!signal.aborted) { resumedFrom = lastTime + let delay = retryDelayMs try { - await tailLogs(client, root, { query, since: lastTime }, (entry) => { - attempt = 0 + await tailLogs(client, root, { query, since: lastTime ?? openedAt }, (entry) => { if (!lastTime || compareTime(entry._time, lastTime) > 0) lastTime = entry._time pending.push(entry) timer ??= setTimeout(flush, FLUSH_MS) - }, signal) + }, signal, () => { + failures = 0 + // With no line yet, the next reconnect resumes from here so the gap is not lost. + openedAt = new Date().toISOString() + update({ state: "live", error: null }) + }) flush() } catch (err) { flush() @@ -84,10 +91,12 @@ export function useLogTail( return } update({ error: err }) + delay = Math.min(retryDelayMs * 2 ** failures, MAX_RETRY_MS) + failures++ } if (signal.aborted) return update({ state: "reconnecting" }) - await sleep(Math.min(retryDelayMs * 2 ** attempt, MAX_RETRY_MS), signal) + await sleep(delay, signal) } } void run()