fix(web): close SSE connections on page unload to prevent connection starvation

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) <[email protected]>
This commit is contained in:
2026-03-26 19:33:18 -07:00
co-authored by Claude Opus 4.6
parent 47f83c1bfc
commit e508d2083b
+8
View File
@@ -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.