Skip to content

Fix memory leak in /ai/analyze_stream SSE endpoint (#3949) - #3958

Open
singhanurag0317-bit wants to merge 1 commit into
riteshbonthalakoti:mainfrom
singhanurag0317-bit:fix/sse-stream-memory-leak
Open

Fix memory leak in /ai/analyze_stream SSE endpoint (#3949)#3958
singhanurag0317-bit wants to merge 1 commit into
riteshbonthalakoti:mainfrom
singhanurag0317-bit:fix/sse-stream-memory-leak

Conversation

@singhanurag0317-bit

Copy link
Copy Markdown

Changes

  • Added
    equest: Request\ parameter to \�nalyze_stream\ endpoint to detect client disconnects
  • Added \�wait request.is_disconnected()\ checks after each yield point — returns early if client is gone, preventing orphaned background processing
  • Wrapped blocking Gemini API calls (\�nalyze_image, \get_summary) in \�syncio.to_thread\ + \�syncio.wait_for\ with 30-second timeout to prevent indefinite blocking
  • Added \ ry/except/finally\ cleanup with \CancelledError\ handling in event generator
  • Added structured log messages for timeouts, errors, and cleanup events

Problem

When clients disconnect mid-stream (page close, network drop), the SSE handler continued running expensive Gemini API calls in the background with no timeout, causing memory leaks and wasted compute.

Solution

Three-layer fix: (1) check disconnect at each step, (2) timeout Gemini calls at 30s, (3) clean up generator on cancellation.

Closes #3949

- Add request.is_disconnected() checks at each yield point to detect client disconnects early and stop streaming
- Wrap blocking Gemini API calls (analyze_image, get_summary) in asyncio.to_thread + asyncio.wait_for with 30s timeout
- Add try/finally/except(CancelledError) cleanup in event_generator
- Add Request parameter to endpoint handler

Closes riteshbonthalakoti#3949
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@singhanurag0317-bit, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 48662022-2020-4211-b6e0-f2cc78b3bf5c

📥 Commits

Reviewing files that changed from the base of the PR and between da8faf2 and a09ffe2.

📒 Files selected for processing (1)
  • backend/main.py
✨ 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.

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.

[Bug]: Resolve Memory Leak in Asynchronous Gemini API SSE Stream Handler

1 participant