Skip to content

fix: enforce thread-safety during reranker initialization #815 - #819

Open
suhaniiz wants to merge 2 commits into
param20h:devfrom
suhaniiz:bug-fix-threadsafety-815
Open

fix: enforce thread-safety during reranker initialization #815#819
suhaniiz wants to merge 2 commits into
param20h:devfrom
suhaniiz:bug-fix-threadsafety-815

Conversation

@suhaniiz

@suhaniiz suhaniiz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

📋 PR Checklist

Thank you for contributing to PDF-Assistant-RAG! 🎉
Please fill out this template before submitting. PRs without it filled in will be closed.


🔗 Related Issue

Closes #815


📝 What does this PR do?

This PR resolves concurrency and race condition vulnerabilities during model initialization in app/reranker.py:

  • It implements two global mutual exclusion locks (_model_lock and _instance_lock) using Python's built-in threading.Lock.
  • It applies a highly efficient Double-Check Locking Pattern inside both the global get_reranker() singleton accessor and the internal lazy-loading _load_model() method.
  • This guarantees that even under heavy, concurrent multi-threaded or asynchronous web traffic (e.g., instant high-volume startup requests via FastAPI/Uvicorn), the heavy cross-encoder model weights are loaded into CPU/GPU memory exactly once.

🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🔧 Refactor / code cleanup
  • 📝 Documentation update
  • 🎨 UI / styling change
  • ⚙️ CI / tooling / config change
  • 🧪 Tests

🧪 How was this tested?

  • Tested the affected API endpoints manually
  • Ran the backend locally (uvicorn app.main:app --reload)
  • Ran the frontend locally (npm run dev inside frontend/)
  • Added / updated tests

📸 Screenshots (if UI change)


⚠️ Anything to flag for reviewers?

The double-check locking mechanic keeps performance optimal because threads only acquire the block lock during the very first initialization window. Once _reranker_instance and _model are populated, subsequent access bypasses the lock entirely, ensuring zero overhead for concurrent production pipelines.


✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@suhaniiz
suhaniiz requested a review from param20h as a code owner July 8, 2026 15:58
@vercel

vercel Bot commented Jul 8, 2026

Copy link
Copy Markdown

@suhaniiz is attempting to deploy a commit to the param20h's projects Team on Vercel.

A member of the Team first needs to authorize it.

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] Race condition and lack of thread-safety during Reranker initialization

1 participant