Skip to content

[Memory Leak] SSE streaming endpoint lacks disconnect detection and task timeout#2878

Open
sanrishi wants to merge 1 commit into
ritesh-1918:gssocfrom
sanrishi:fix/sse-disconnect-timeout
Open

[Memory Leak] SSE streaming endpoint lacks disconnect detection and task timeout#2878
sanrishi wants to merge 1 commit into
ritesh-1918:gssocfrom
sanrishi:fix/sse-disconnect-timeout

Conversation

@sanrishi

Copy link
Copy Markdown

Target

\�ackend/routers/ai.py\ : Lines 321–471 (\�nalyze_stream\ endpoint)

Vector

Resource Memory Leak (Performance)

Problem

The /ai/analyze_stream\ SSE endpoint creates an async generator that runs indefinitely without:

  1. Client disconnect detection: No
    equest.is_disconnected()\ checks. Abandoned generators hold model references until completion.
  2. Timeout protection: No max execution timeout. A frozen client keeps the generator alive, blocking resources.
  3. Task cancellation: \�syncio.sleep()\ calls have no cancellation wrapping.

Changes

  • Added _sse_disconnect_guard()\ helper checking
    equest.is_disconnected()\ before each yield
  • Added _check_timeout()\ with 60s max window and early return
  • Wrapped \�syncio.sleep()\ with \�syncio.wait_for()\ and 5s timeout
  • Added try/finally cleanup logging on generator cancellation

Fixes #2868

- Add _sse_disconnect_guard() helper to detect client drops
- Add _check_timeout() for 60s max execution window
- Wrap asyncio.sleep() calls with wait_for timeout
- Add try/finally cleanup for cancelled generators

Fixes ritesh-1918#2868
@vercel

vercel Bot commented Jun 14, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the ritesh Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c068ba8c-7743-4885-9411-49015f4f3dc1

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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.

1 participant