Skip to content

fix(backend): Phase 2.3 SSE heartbeat leak on disconnect - #666

Merged
LucasSantana-Dev merged 12 commits into
mainfrom
fix/phase-2-sse-heartbeat-leak
Apr 17, 2026
Merged

LucasSantana-Dev merged 12 commits into
mainfrom
fix/phase-2-sse-heartbeat-leak

Conversation

@LucasSantana-Dev

Copy link
Copy Markdown
Owner

Bug

Heartbeat interval on SSE connections leaks when client disconnects mid-send. The setInterval keeps firing and its res.write(...) throws on a closed socket, producing error spam — and if the interval holds refs to per-client state, memory leaks accumulate.

Fix

Wrapped heartbeat scheduling in defensive guards:

  • Create const controller = new AbortController() per SSE client
  • When req emits close, call controller.abort() and clearInterval(heartbeat)
  • Guard res.write calls: check controller.signal.aborted || res.writableEnded || res.destroyed before writing, skip and return if true

Files Changed

  • packages/backend/src/routes/music/stateRoutes.ts: Added AbortController and guards to heartbeat interval
  • packages/backend/tests/integration/routes/musicState.test.ts: Added regression test for early disconnect

Test Results

  • All 8 SSE stream tests pass, including new 'does not write heartbeat after client disconnect' test
  • Full backend test suite: 60 suites, 751 tests passed, 0 failed
  • No TypeScript or linting errors

Risk

Very low — pure defensive guards added, no behavioral changes. Interval now skips write attempts on dead sockets instead of throwing.

@LucasSantana-Dev
LucasSantana-Dev enabled auto-merge (squash) April 16, 2026 23:15
@vercel

vercel Bot commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lucky Ready Ready Preview, Comment Apr 17, 2026 0:39am

Request Review

@coderabbitai

coderabbitai Bot commented Apr 16, 2026 •

Copy link
Copy Markdown

Warning

Rate limit exceeded

@LucasSantana-Dev has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 59 minutes and 56 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 59 minutes and 56 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: df387bb9-f932-40de-a496-321a1aebbee4

📥 Commits

Reviewing files that changed from the base of the PR and between 2baf9ee and 28ba9ee.

📒 Files selected for processing (2)
  • packages/backend/src/routes/music/stateRoutes.ts
  • packages/backend/tests/integration/routes/musicState.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/phase-2-sse-heartbeat-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@LucasSantana-Dev
LucasSantana-Dev merged commit 8e73c12 into main Apr 17, 2026
12 checks passed
LucasSantana-Dev added a commit that referenced this pull request May 13, 2026
* fix(backend): guard SSE heartbeat against client disconnect leak

* test(backend): expand SSE heartbeat leak coverage

* test(backend): add timer cleanup and additional SSE heartbeat tests

* test(backend): remove flaky test relying on 30s interval

* chore: add NOSONAR comments to exception handlers in SSE heartbeat

* test(backend): cover SSE heartbeat exception paths for sonar
@LucasSantana-Dev
LucasSantana-Dev deleted the fix/phase-2-sse-heartbeat-leak branch May 23, 2026 02:21

This branch was successfully deployed

1 active deployment
Preview — 28ba9eea Deployed Apr 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant