diff --git a/packages/ui/src/lib/terminalApi.ts b/packages/ui/src/lib/terminalApi.ts index 2a2df802..99c38835 100644 --- a/packages/ui/src/lib/terminalApi.ts +++ b/packages/ui/src/lib/terminalApi.ts @@ -786,7 +786,6 @@ const connectTerminalStreamViaSse = ( let retryTimeout: ReturnType | null = null; let connectionTimeoutId: ReturnType | null = null; let isClosed = false; - let hasDispatchedOpen = false; let terminalExited = false; const clearTimeouts = () => { @@ -850,20 +849,21 @@ const connectTerminalStreamViaSse = ( } eventSource = new EventSource(`/api/terminal/${sessionId}/stream`); + let opened = false; connectionTimeoutId = setTimeout(() => { - if (!hasDispatchedOpen && eventSource?.readyState !== EventSource.OPEN) { + if (!opened && eventSource?.readyState !== EventSource.OPEN) { eventSource?.close(); handleError(new Error('Connection timeout'), false); } }, connectionTimeout); eventSource.onopen = () => { - if (hasDispatchedOpen) { + if (opened) { return; } - hasDispatchedOpen = true; + opened = true; retryCount = 0; clearTimeouts(); onEvent({ type: 'connected' });