Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: duplicated project/container logs when refreshing log viewer (#1620)
Kyle Mendell2ef86f64398d
diff --git a/frontend/src/lib/components/logs/log-viewer.svelte b/frontend/src/lib/components/logs/log-viewer.svelteindex acacbd986..a88793454 100644--- a/frontend/src/lib/components/logs/log-viewer.svelte+++ b/frontend/src/lib/components/logs/log-viewer.svelte@@ -104,7 +104,7 @@});}- export function clearLogs(opts?: { hard?: boolean; restart?: boolean }) {+ export async function clearLogs(opts?: { hard?: boolean; restart?: boolean }) {const hard = opts?.hard === true;if (hard) {@@ -120,7 +120,7 @@onClear?.();if (opts?.restart) {- stopLogStream();+ await stopLogStream();startLogStream();}}@@ -132,10 +132,13 @@let logContainer: HTMLElement | undefined = $state();let isStreaming = $state(false);let shouldBeStreaming = $state(false);+ let connecting = $state(false);let error: string | null = $state(null);let eventSource: EventSource | null = null;let wsClient: ReconnectingWebSocket<string> | null = null;let currentStreamKey: string | null = null;+ let streamSession = 0;+ let currentStreamSession = 0;function streamKey() {return type === 'project' ? (projectId ? `project:${projectId}` : null) : containerId ? `ctr:${containerId}` : null;}@@ -167,15 +170,18 @@return;}- // Prevent starting if already streaming- if (shouldBeStreaming && wsClient) {+ // Prevent starting if already streaming or connecting+ if (connecting || (shouldBeStreaming && wsClient)) {return;}+ connecting = true;+ const sessionId = ++streamSession;+ currentStreamSession = sessionId;try {shouldBeStreaming = true;error = null;- await startWebSocketStream();+ await startWebSocketStream(sessionId);// Only notify after successful startisStreaming = true;onStart?.();@@ -185,10 +191,15 @@error = m.log_stream_failed_connect({ type: humanType });isStreaming = false;shouldBeStreaming = false;+ if (currentStreamSession === sessionId) {+ currentStreamSession = 0;+ }+ } finally {+ connecting = false;}}- async function startWebSocketStream() {+ async function startWebSocketStream(sessionId: number) {// Close existing connection if anyif (wsClient) {try {@@ -210,11 +221,13 @@}},onOpen: () => {+ if (sessionId !== currentStreamSession) return;if (dev) console.log(m.log_viewer_connected({ type: humanType }));error = null;isStreaming = true;},onMessage: (payload) => {+ if (sessionId !== currentStreamSession) return;if (!payload) return;if (Array.isArray(payload)) {for (const obj of payload) processLogObject(obj);@@ -223,16 +236,18 @@}},onError: (e) => {+ if (sessionId !== currentStreamSession) return;console.error('WebSocket log stream error:', e);error = m.log_stream_connection_lost({ type: humanType });},onClose: () => {+ if (sessionId !== currentStreamSession) return;isStreaming = false;if (!error && shouldBeStreaming) {error = m.log_stream_closed_by_server({ type: humanType });}},- shouldReconnect: () => shouldBeStreaming,+ shouldReconnect: () => shouldBeStreaming && sessionId === currentStreamSession,maxBackoff: 10000});@@ -252,24 +267,29 @@});}- export function stopLogStream(notifyCallback = true) {+ export async function stopLogStream(notifyCallback = true): Promise<void> {shouldBeStreaming = false;+ currentStreamSession = 0;if (eventSource) {if (dev) console.log(m.log_viewer_stopping({ type: humanType }));eventSource.close();eventSource = null;}++ let closePromise: Promise<void> | undefined;if (wsClient) {- try {- wsClient.close();- } catch {}+ closePromise = wsClient.closeAndWait?.();wsClient = null;}+isStreaming = false;+ connecting = false;if (notifyCallback) {onStop?.();}++ await closePromise;}function addLogEntry(logData: { level: string; message: string; timestamp?: string; service?: string; containerId?: string }) {@@ -370,13 +390,18 @@// If key changed while streaming, restart with new keyif (currentStreamKey && currentStreamKey !== key) {const wasStreaming = shouldBeStreaming;- if (wasStreaming) {- stopLogStream(false);- }- logs = [];currentStreamKey = key;+if (wasStreaming) {- startLogStream();+ (async () => {+ await stopLogStream(false);+ logs = [];+ if (currentStreamKey === key) {+ startLogStream();+ }+ })();+ } else {+ logs = [];}} else if (!currentStreamKey) {// First time - just set the key, don't auto-startdiff --git a/frontend/src/lib/utils/ws.ts b/frontend/src/lib/utils/ws.tsindex edd69ab63..7e0c88aef 100644--- a/frontend/src/lib/utils/ws.ts+++ b/frontend/src/lib/utils/ws.ts@@ -155,6 +155,48 @@ export class ReconnectingWebSocket<T = unknown> {this.connecting = false;}+ closeAndWait(timeoutMs = 2000) {+ this.closed = true;+ this.attempt = 0;++ if (this.reconnectTimer) {+ clearTimeout(this.reconnectTimer);+ this.reconnectTimer = null;+ }++ const socket = this.ws;+ if (!socket || socket.readyState === WebSocket.CLOSED) {+ this.ws = null;+ this.connecting = false;+ return Promise.resolve();+ }++ return new Promise<void>((resolve) => {+ let timeout: ReturnType<typeof setTimeout> | null = null;+ const finalize = () => {+ if (timeout) clearTimeout(timeout);+ socket.removeEventListener('close', handleClose);+ if (socket === this.ws) {+ this.ws = null;+ }+ this.connecting = false;+ resolve();+ };+ const handleClose = () => finalize();++ socket.addEventListener('close', handleClose, { once: true });+ try {+ socket.close();+ } catch {+ // Ignore close errors; rely on timeout/close event.+ }++ if (timeoutMs > 0) {+ timeout = setTimeout(finalize, timeoutMs);+ }+ });+ }+isConnected() {return !!this.ws && this.ws.readyState === WebSocket.OPEN;}diff --git a/frontend/src/routes/(app)/containers/components/ContainerLogsPanel.svelte b/frontend/src/routes/(app)/containers/components/ContainerLogsPanel.svelteindex 90a328fbb..072cbefd7 100644--- a/frontend/src/routes/(app)/containers/components/ContainerLogsPanel.svelte+++ b/frontend/src/routes/(app)/containers/components/ContainerLogsPanel.svelte@@ -40,11 +40,8 @@onClear();}- function handleRefresh() {- viewer?.stopLogStream();- setTimeout(() => {- viewer?.startLogStream();- }, 100);+ async function handleRefresh() {+ await viewer?.clearLogs({ hard: true, restart: true });}// Sync isStreaming from viewer callbacksdiff --git a/frontend/src/routes/(app)/projects/components/ProjectLogsPanel.svelte b/frontend/src/routes/(app)/projects/components/ProjectLogsPanel.svelteindex 43828aa7b..015eb9b09 100644--- a/frontend/src/routes/(app)/projects/components/ProjectLogsPanel.svelte+++ b/frontend/src/routes/(app)/projects/components/ProjectLogsPanel.svelte@@ -34,9 +34,8 @@viewer?.clearLogs();}- function handleRefresh() {- viewer?.stopLogStream();- viewer?.startLogStream();+ async function handleRefresh() {+ await viewer?.clearLogs({ hard: true, restart: true });}$effect(() => {@@ -97,7 +96,6 @@height="calc(100vh - 320px)"onStart={handleStart}onStop={handleStop}- onClear={handleClear}/></Card.Content></Card.Root>