Skip to content

fix(#850): EventSource reconnect storm + backoff timing - #1170

Merged
ogazboiz merged 2 commits into
LabsCrypt:mainfrom
Hollujay:fix-850-clean
Aug 6, 2026
Merged

fix(#850): EventSource reconnect storm + backoff timing#1170
ogazboiz merged 2 commits into
LabsCrypt:mainfrom
Hollujay:fix-850-clean

Conversation

@Hollujay

@Hollujay Hollujay commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Closes #850

Summary

Fixes the EventSource reconnect storm in useStreamEvents — supersedes #996 with a scoped-down diff per @ogazboiz's review. This PR touches only the two files directly related to the hook fix:

  • frontend/src/hooks/useStreamEvents.ts
  • frontend/src/__tests__/useStreamEvents.test.tsx

No CI workflow edits, formatting changes, logger/dashboard changes, or backend test tweaks are included — those were dropped from #996 as unrelated scope creep.

What changed

Reconnect storm fix:

  • Memoized the subscription key (streamIds + subscribeToAll + jwtToken) so buildUrl doesn't get a new reference on every render, which previously caused the effect to reconnect even when the actual subscription hadn't changed.
  • connect() now clears any pending reconnect timeout at the start, preventing overlapping/duplicate connections.
  • Added a MAX_RECONNECT_ATTEMPTS cap (20) so a persistently failing connection stops retrying instead of looping forever.

Combined with an independent fix already on main:
While this branch was open, main picked up a separate fix for the backoff-delay timing (precomputing the capped delay and the next backoff value before scheduling setTimeout, rather than updating it after connectRef.current() already ran). Both fixes touch the same connect()/onerror block, so this PR merges them together rather than one overwriting the other — see useStreamEvents.ts for the combined logic.

Testing

  • useStreamEvents.test.tsx now includes all 14 relevant tests: the original hook tests, main's 3 backoff-timing tests (exponential growth, cap at maxRetryDelay, reset after successful reconnect), and this PR's 2 storm-prevention tests (single EventSource instance across re-renders, stops reconnecting after hitting the cap).
  • All 14 tests pass locally.
  • Verified via tsc --noEmit that this change introduces zero new type errors compared to a clean main checkout.

Notes for reviewers

- Memoize subscription key so identical streamIds/token don't
  trigger spurious reconnects on every render.
- Clear any pending reconnect timeout at the start of connect()
  to prevent overlapping connections.
- Cap reconnect attempts at 20 to stop infinite reconnect loops.
- Combined with an independent backoff-timing fix already on main
  (precompute delay before scheduling, not after).
# Conflicts:
#	frontend/src/hooks/useStreamEvents.ts
@ogazboiz
ogazboiz merged commit 702c765 into LabsCrypt:main Aug 6, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Frontend] useStreamEvents tears down and recreates the EventSource on every render - SSE reconnect storm/leak

2 participants