From e508d2083b196e38a92aa1b7d291cd30dc441246 Mon Sep 17 00:00:00 2001 From: "ilblackdragon@gmail.com" Date: Thu, 26 Mar 2026 19:33:18 -0700 Subject: [PATCH] fix(web): close SSE connections on page unload to prevent connection starvation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The browser limits concurrent HTTP/1.1 connections per origin to 6. Without cleanup, SSE connections from prior page loads linger after refresh/navigation, eating into the pool. After 2-3 refreshes, all 6 slots are consumed by stale SSE streams and new API fetch calls queue indefinitely — the UI shows "connected" (SSE works) but data never loads. Add a beforeunload handler that closes both eventSource (chat events) and logEventSource (log stream) so the browser can reuse connections immediately on page reload. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/channels/web/static/app.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/channels/web/static/app.js b/src/channels/web/static/app.js index a96cb794..3c588f0e 100644 --- a/src/channels/web/static/app.js +++ b/src/channels/web/static/app.js @@ -215,6 +215,14 @@ document.getElementById('token-input').addEventListener('keydown', (e) => { if (e.key === 'Enter') authenticate(); }); +// Close SSE connections on page unload to free the browser's connection pool. +// Without this, stale SSE connections from prior page loads linger and exhaust +// the HTTP/1.1 per-origin connection limit (6), blocking API fetch calls. +window.addEventListener('beforeunload', () => { + if (eventSource) { eventSource.close(); eventSource = null; } + if (logEventSource) { logEventSource.close(); logEventSource = null; } +}); + // Note: main event listener registration is at the bottom of this file (search // "Event Listener Registration"). Do NOT add duplicate listeners here.