fix(rag): retrieve session details inside lock to prevent KeyError crash during PDF upload response construction#571
Conversation
…ash during PDF upload response construction
|
@Sandeep6135 is attempting to deploy a commit to the firefistisdead's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Warning Review limit reached
More reviews will be available in 45 minutes and 23 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the 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 credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. 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, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Pull request overview
This PR targets a concurrency bug in the RAG service’s /process-pdf handler where building the success response could crash with a KeyError if the session entry was evicted from the global sessions map between unlocking and response serialization.
Changes:
- Retrieves the session object via
sessions.get(session_id)undersessions_lockinstead of indexingsessions[session_id]. - Captures
session_secretanddocumentswhile holdingsessions_lockand reusessession_secretin the response payload.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| with sessions_lock: | ||
| documents = list(sessions[session_id].get("documents", [])) | ||
| session = sessions.get(session_id) | ||
| session_secret = session.get("session_secret") if session else None | ||
| documents = list(session.get("documents", [])) if session else [] |
| with sessions_lock: | ||
| documents = list(sessions[session_id].get("documents", [])) | ||
| session = sessions.get(session_id) | ||
| session_secret = session.get("session_secret") if session else None | ||
| documents = list(session.get("documents", [])) if session else [] |
Pull Request: Resolve Uncaught KeyError and Worker Crash during PDF Upload Ingestion
Fixes #564
📌 Classification & Priority
bug-fixhigh/criticalrag-serviceexceptional-crash-prevention📖 Summary
Important
This PR fixes a critical unhandled exception defect in the
process_pdfendpoint that crashed the worker process due to aKeyErrorwhen constructing the response payload for a processed PDF under high concurrency or memory eviction limits.🔴 Problem
During PDF upload processing, the response construction accessed the global
sessionsdictionary at line 3326:"session_secret": sessions[session_id].get("session_secret")This access was performed outside the protection of the
sessions_lock. Under high concurrency or cache eviction limits (MAX_ACTIVE_SESSIONS), the session could get evicted from memory by a concurrent thread immediately after the lock was released. This resulted in an uncaughtKeyError: session_idwhich crashed the request handler.🟢 Solution
Refactored the response construction block:
documentsandsession_secretinside thewith sessions_lock:block.get()fetches (sessions.get(session_id)) to handle potentially missing/evicted sessions gracefully instead of throwing aKeyErrorif the session gets garbage collected right before response serialization.🧪 Steps to Reproduce
MAX_ACTIVE_SESSIONS).KeyErroron line 3326 and failure to return an API response.🔍 Expected Behaviour
The API responds with the processed document metadata without crashing or throwing unhandled collection key errors.
❌ Actual Behaviour (Before Fix)
The Python worker process encountered a
KeyErrorand crashed during response dictionary compilation, failing the upload request.🛠️ Code Diff Walkthrough
rag-service/main.py