Notification SSE stream retries infinitely when session expires #203
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Context
NotificationBell.svelteopens anEventSourceto/api/notifications/streamon mount (line 130). The browser'sEventSourceAPI automatically reconnects on any error -- this is by design for transient network failures, but becomes a problem when the server returns 401/403 due to an expired session.What happens
EventSourcereconnect fires a request to/api/notifications/streamSecurityConfig.javaline 63:auth.anyRequest().authenticated())EventSourcesees an error, waits ~3 seconds, retries -- foreverImpact
Root cause
NotificationBell.sveltelines 128-137 -- theEventSourcesetup has noonerrorhandler:The
EventSourceAPI does not expose the HTTP status code in itsonerrorevent, so the client cannot distinguish a 401 from a network hiccup purely from the error event. However,EventSource.readyState === EventSource.CLOSEDindicates the browser gave up (which happens on non-retryable errors like 401 in most browsers), vsEventSource.CONNECTINGwhich means it is retrying.Proposed fix
Add an
onerrorhandler that:eventSource.readyState-- ifCLOSED, the server rejected the connection (likely 401). Close and stop.CONNECTING(browser is retrying), probe the session with a lightweight fetch to/api/notifications/unread-count. If that returns 401, close theEventSourceand redirect to/login.Files involved
frontend/src/lib/components/NotificationBell.svelte-- needsonerrorhandler (primary fix)backend/src/main/java/org/raddatz/familienarchiv/service/SseEmitterRegistry.java-- no changes needed, already cleans up on error/timeoutbackend/src/main/java/org/raddatz/familienarchiv/config/SecurityConfig.java-- no changes needed