Skip to content

fix: warm instructor mode registry to prevent thread-safety race condition#273

Closed
bsatapat-jpg wants to merge 1 commit into
lightspeed-core:mainfrom
bsatapat-jpg:dev_1
Closed

fix: warm instructor mode registry to prevent thread-safety race condition#273
bsatapat-jpg wants to merge 1 commit into
lightspeed-core:mainfrom
bsatapat-jpg:dev_1

Conversation

@bsatapat-jpg

@bsatapat-jpg bsatapat-jpg commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Description

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Unit tests improvement

Tools used to create PR

Identify any AI code assistants used in this PR (for transparency and review context)

  • Assisted-by: Claude-4.6-opus-high
  • Generated by: Cursor

Related Tickets & Documents

  • Related Issue #
  • Closes #

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

  • Please provide detailed steps to perform tests related to this code change.
  • How were the fix/results from this change verified? Please provide relevant screenshots or results.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when loading evaluation components by preloading required handlers at startup.
    • Reduced the chance of intermittent failures in multi-threaded environments when components are accessed concurrently.
    • Added safer handling so missing optional dependencies no longer interrupt startup.

@bsatapat-jpg bsatapat-jpg changed the title fix: warm instructor mode registry to prevent thread-safety race cond… fix: warm instructor mode registry to prevent thread-safety race condition Jun 30, 2026
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

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

Next review available in: 22 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b003a0fe-d72c-4f9d-a365-ee30c47b152d

📥 Commits

Reviewing files that changed from the base of the PR and between b4e954c and dbd3a28.

📒 Files selected for processing (1)
  • src/lightspeed_evaluation/core/metrics/ragas.py

Walkthrough

Adds a _warm_instructor_registry helper in ragas.py that eagerly iterates Instructor's mode_registry._lazy_loaders and calls get_handlers for each key at module import time, suppressing KeyError/ImportError, to prevent a thread-unsafe lazy-loading race in Instructor v1.15+.

Changes

Instructor Registry Warmup

Layer / File(s) Summary
Import-time registry warmup
src/lightspeed_evaluation/core/metrics/ragas.py
Defines and immediately calls _warm_instructor_registry, which iterates mode_registry._lazy_loaders and calls mode_registry.get_handlers for each key, suppressing KeyError and ImportError per key and swallowing top-level ImportError if Instructor is absent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main fix: warming the Instructor mode registry to avoid a thread-safety race condition.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lightspeed_evaluation/core/metrics/ragas.py`:
- Around line 41-50: The Instructor warmup in ragas.py relies on the private
mode_registry._lazy_loaders attribute and silently swallows skipped handlers.
Update the warmup to access _lazy_loaders via getattr(mode_registry,
"_lazy_loaders", None) before iterating, and in the
mode_registry.get_handlers(...) exception path replace the silent pass with a
debug log for KeyError and ImportError so skipped warmups are visible. Use the
existing import-time warmup block around mode_registry and get_handlers to
locate the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d9295ffd-79ac-4511-a7a5-63350f71cc54

📥 Commits

Reviewing files that changed from the base of the PR and between ab54c98 and b4e954c.

📒 Files selected for processing (1)
  • src/lightspeed_evaluation/core/metrics/ragas.py

Comment thread src/lightspeed_evaluation/core/metrics/ragas.py Outdated
logger = logging.getLogger(__name__)


def _warm_instructor_registry() -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another workaround - we need to spend some time to redesign this..
How do we make sure it will work always ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is in instructor>=1.15 — their ModeRegistry.get_handlers() does a non-atomic pop() + load() sequence without a lock.
Once instructlab fixes it, we can remove it out.
Wdyt?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing a version bump here #274 - check if that resolves this issue

@asamal4

asamal4 commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

@bsatapat-jpg Can we close this PR, as per your comment the issue is fixed by the version bump in #274

@bsatapat-jpg

Copy link
Copy Markdown
Collaborator Author

@bsatapat-jpg Can we close this PR, as per your comment the issue is fixed by the version bump in #274

Yups it's fixed the issues. We can close it.

@asamal4 asamal4 closed this Jun 30, 2026
@bsatapat-jpg bsatapat-jpg deleted the dev_1 branch July 1, 2026 09:51
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.

2 participants